<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>2.3 常用函数 | 商业数据分析师-R 语言数据处理</title>
  <meta name="description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  <meta name="generator" content="bookdown 0.22 and GitBook 2.6.7" />

  <meta property="og:title" content="2.3 常用函数 | 商业数据分析师-R 语言数据处理" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="2.3 常用函数 | 商业数据分析师-R 语言数据处理" />
  
  <meta name="twitter:description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  

<meta name="author" content="Yufei Zhong" />


<meta name="date" content="2021-06-23" />

  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="dplyr-usage.html"/>
<link rel="next" href="dplyr-merge-two-table.html"/>
<script src="libs/header-attrs-2.8/header-attrs.js"></script>
<script src="libs/jquery-3.5.1/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />









<link href="libs/anchor-sections-1.0.1/anchor-sections.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.0.1/anchor-sections.js"></script>
<link href="libs/tabwid-1.0.0/tabwid.css" rel="stylesheet" />
<script src="libs/htmlwidgets-1.5.3/htmlwidgets.js"></script>
<link href="libs/datatables-css-0.0.0/datatables-crosstalk.css" rel="stylesheet" />
<script src="libs/datatables-binding-0.18/datatables.js"></script>
<link href="libs/dt-core-1.10.20/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="libs/dt-core-1.10.20/css/jquery.dataTables.extra.css" rel="stylesheet" />
<script src="libs/dt-core-1.10.20/js/jquery.dataTables.min.js"></script>
<link href="libs/crosstalk-1.1.1/css/crosstalk.css" rel="stylesheet" />
<script src="libs/crosstalk-1.1.1/js/crosstalk.min.js"></script>


<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>


<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">R学习笔记</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>欢迎</a>
<ul>
<li class="chapter" data-level="" data-path="abstract.html"><a href="abstract.html"><i class="fa fa-check"></i>内容概要</a></li>
<li class="chapter" data-level="" data-path="sec-licenses.html"><a href="sec-licenses.html"><i class="fa fa-check"></i>授权说明</a></li>
<li class="chapter" data-level="" data-path="session.html"><a href="session.html"><i class="fa fa-check"></i>运行信息</a></li>
<li class="chapter" data-level="" data-path="author.html"><a href="author.html"><i class="fa fa-check"></i>关于本人</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="data-read-write-description.html"><a href="data-read-write-description.html"><i class="fa fa-check"></i><b>1</b> 数据导入导出</a>
<ul>
<li class="chapter" data-level="1.1" data-path="readxl-description.html"><a href="readxl-description.html"><i class="fa fa-check"></i><b>1.1</b> readxl</a>
<ul>
<li class="chapter" data-level="1.1.1" data-path="readxl-description.html"><a href="readxl-description.html#readxl:install"><i class="fa fa-check"></i><b>1.1.1</b> 安装</a></li>
<li class="chapter" data-level="1.1.2" data-path="readxl-description.html"><a href="readxl-description.html#readxl:usage"><i class="fa fa-check"></i><b>1.1.2</b> 用法</a></li>
<li class="chapter" data-level="1.1.3" data-path="readxl-description.html"><a href="readxl-description.html#batch:read"><i class="fa fa-check"></i><b>1.1.3</b> 批量读取</a></li>
<li class="chapter" data-level="1.1.4" data-path="readxl-description.html"><a href="readxl-description.html#batch:write"><i class="fa fa-check"></i><b>1.1.4</b> 批量输出</a></li>
</ul></li>
<li class="chapter" data-level="1.2" data-path="writexl.html"><a href="writexl.html"><i class="fa fa-check"></i><b>1.2</b> writexl</a>
<ul>
<li class="chapter" data-level="1.2.1" data-path="writexl.html"><a href="writexl.html#writexl:usage"><i class="fa fa-check"></i><b>1.2.1</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="openxlsx-description.html"><a href="openxlsx-description.html"><i class="fa fa-check"></i><b>1.3</b> openxlsx</a>
<ul>
<li class="chapter" data-level="1.3.1" data-path="openxlsx-description.html"><a href="openxlsx-description.html#openxlsx:install"><i class="fa fa-check"></i><b>1.3.1</b> 安装</a></li>
<li class="chapter" data-level="1.3.2" data-path="openxlsx-description.html"><a href="openxlsx-description.html#openxlsx:functions"><i class="fa fa-check"></i><b>1.3.2</b> 基础功能</a></li>
<li class="chapter" data-level="1.3.3" data-path="openxlsx-description.html"><a href="openxlsx-description.html#定制格式输出"><i class="fa fa-check"></i><b>1.3.3</b> 定制格式输出</a></li>
<li class="chapter" data-level="1.3.4" data-path="openxlsx-description.html"><a href="openxlsx-description.html#函数参数"><i class="fa fa-check"></i><b>1.3.4</b> 函数参数</a></li>
<li class="chapter" data-level="1.3.5" data-path="openxlsx-description.html"><a href="openxlsx-description.html#总结"><i class="fa fa-check"></i><b>1.3.5</b> 总结</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="readr-package.html"><a href="readr-package.html"><i class="fa fa-check"></i><b>1.4</b> readr</a>
<ul>
<li class="chapter" data-level="1.4.1" data-path="readr-package.html"><a href="readr-package.html#readr:install"><i class="fa fa-check"></i><b>1.4.1</b> 安装</a></li>
<li class="chapter" data-level="1.4.2" data-path="readr-package.html"><a href="readr-package.html#readr:usage"><i class="fa fa-check"></i><b>1.4.2</b> 用法</a></li>
<li class="chapter" data-level="1.4.3" data-path="readr-package.html"><a href="readr-package.html#readr:write-function"><i class="fa fa-check"></i><b>1.4.3</b> 导出功能</a></li>
<li class="chapter" data-level="1.4.4" data-path="readr-package.html"><a href="readr-package.html#总结-1"><i class="fa fa-check"></i><b>1.4.4</b> 总结</a></li>
</ul></li>
<li class="chapter" data-level="1.5" data-path="data-vroom.html"><a href="data-vroom.html"><i class="fa fa-check"></i><b>1.5</b> vroom</a>
<ul>
<li class="chapter" data-level="1.5.1" data-path="data-vroom.html"><a href="data-vroom.html#安装"><i class="fa fa-check"></i><b>1.5.1</b> 安装</a></li>
<li class="chapter" data-level="1.5.2" data-path="data-vroom.html"><a href="data-vroom.html#用法"><i class="fa fa-check"></i><b>1.5.2</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="1.6" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html"><i class="fa fa-check"></i><b>1.6</b> Rstudio导入</a>
<ul>
<li class="chapter" data-level="1.6.1" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html#rstudio-工具栏导入"><i class="fa fa-check"></i><b>1.6.1</b> rstudio 工具栏导入</a></li>
<li class="chapter" data-level="1.6.2" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html#插件导入"><i class="fa fa-check"></i><b>1.6.2</b> 插件导入</a></li>
</ul></li>
<li class="chapter" data-level="1.7" data-path="data-file-path.html"><a href="data-file-path.html"><i class="fa fa-check"></i><b>1.7</b> 文件路径</a>
<ul>
<li class="chapter" data-level="1.7.1" data-path="data-file-path.html"><a href="data-file-path.html#指定路径"><i class="fa fa-check"></i><b>1.7.1</b> 指定路径</a></li>
<li class="chapter" data-level="1.7.2" data-path="data-file-path.html"><a href="data-file-path.html#默认路径"><i class="fa fa-check"></i><b>1.7.2</b> 默认路径</a></li>
</ul></li>
<li class="chapter" data-level="1.8" data-path="data-expand.html"><a href="data-expand.html"><i class="fa fa-check"></i><b>1.8</b> 拓展</a>
<ul>
<li class="chapter" data-level="1.8.1" data-path="data-expand.html"><a href="data-expand.html#tidyxl:package"><i class="fa fa-check"></i><b>1.8.1</b> tidyxl</a></li>
<li class="chapter" data-level="1.8.2" data-path="data-expand.html"><a href="data-expand.html#参考资料"><i class="fa fa-check"></i><b>1.8.2</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="Data-Manipulation-dplyr.html"><a href="Data-Manipulation-dplyr.html"><i class="fa fa-check"></i><b>2</b> 数据处理之-dplyr</a>
<ul>
<li class="chapter" data-level="2.1" data-path="dplyr-description.html"><a href="dplyr-description.html"><i class="fa fa-check"></i><b>2.1</b> 前言</a>
<ul>
<li class="chapter" data-level="2.1.1" data-path="dplyr-description.html"><a href="dplyr-description.html#dplyr:install-package"><i class="fa fa-check"></i><b>2.1.1</b> 安装</a></li>
<li class="chapter" data-level="2.1.2" data-path="dplyr-description.html"><a href="dplyr-description.html#dplyr:difference-of-sql"><i class="fa fa-check"></i><b>2.1.2</b> Excel and Sql 类比</a></li>
<li class="chapter" data-level="2.1.3" data-path="dplyr-description.html"><a href="dplyr-description.html#常见问题"><i class="fa fa-check"></i><b>2.1.3</b> 常见问题</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="dplyr-usage.html"><a href="dplyr-usage.html"><i class="fa fa-check"></i><b>2.2</b> 基础用法</a>
<ul>
<li class="chapter" data-level="2.2.1" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-filter"><i class="fa fa-check"></i><b>2.2.1</b> filter</a></li>
<li class="chapter" data-level="2.2.2" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-select"><i class="fa fa-check"></i><b>2.2.2</b> select</a></li>
<li class="chapter" data-level="2.2.3" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-rename"><i class="fa fa-check"></i><b>2.2.3</b> rename</a></li>
<li class="chapter" data-level="2.2.4" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-relocate"><i class="fa fa-check"></i><b>2.2.4</b> relocate</a></li>
<li class="chapter" data-level="2.2.5" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-mutate"><i class="fa fa-check"></i><b>2.2.5</b> mutate</a></li>
<li class="chapter" data-level="2.2.6" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-arrange"><i class="fa fa-check"></i><b>2.2.6</b> arrange</a></li>
<li class="chapter" data-level="2.2.7" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-summarise"><i class="fa fa-check"></i><b>2.2.7</b> summarise</a></li>
<li class="chapter" data-level="2.2.8" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-groupby"><i class="fa fa-check"></i><b>2.2.8</b> group_by</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="dplyr-functions.html"><a href="dplyr-functions.html"><i class="fa fa-check"></i><b>2.3</b> 常用函数</a>
<ul>
<li class="chapter" data-level="2.3.1" data-path="dplyr-functions.html"><a href="dplyr-functions.html#条件判断"><i class="fa fa-check"></i><b>2.3.1</b> 条件判断</a></li>
<li class="chapter" data-level="2.3.2" data-path="dplyr-functions.html"><a href="dplyr-functions.html#case_when"><i class="fa fa-check"></i><b>2.3.2</b> case_when</a></li>
<li class="chapter" data-level="2.3.3" data-path="dplyr-functions.html"><a href="dplyr-functions.html#计数函数"><i class="fa fa-check"></i><b>2.3.3</b> 计数函数</a></li>
<li class="chapter" data-level="2.3.4" data-path="dplyr-functions.html"><a href="dplyr-functions.html#排序函数"><i class="fa fa-check"></i><b>2.3.4</b> 排序函数</a></li>
<li class="chapter" data-level="2.3.5" data-path="dplyr-functions.html"><a href="dplyr-functions.html#提取向量"><i class="fa fa-check"></i><b>2.3.5</b> 提取向量</a></li>
<li class="chapter" data-level="2.3.6" data-path="dplyr-functions.html"><a href="dplyr-functions.html#dplyr:slice"><i class="fa fa-check"></i><b>2.3.6</b> slice 系列</a></li>
<li class="chapter" data-level="2.3.7" data-path="dplyr-functions.html"><a href="dplyr-functions.html#group-系列"><i class="fa fa-check"></i><b>2.3.7</b> group 系列</a></li>
<li class="chapter" data-level="2.3.8" data-path="dplyr-functions.html"><a href="dplyr-functions.html#其它函数"><i class="fa fa-check"></i><b>2.3.8</b> 其它函数</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html"><i class="fa fa-check"></i><b>2.4</b> 表关联</a>
<ul>
<li class="chapter" data-level="2.4.1" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html#dplyr:merge-table-usage"><i class="fa fa-check"></i><b>2.4.1</b> 两表关联</a></li>
<li class="chapter" data-level="2.4.2" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html#dplyr:merge-multiple-table"><i class="fa fa-check"></i><b>2.4.2</b> 多表操作</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html"><i class="fa fa-check"></i><b>2.5</b> 分组操作</a>
<ul>
<li class="chapter" data-level="2.5.1" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#添加分组"><i class="fa fa-check"></i><b>2.5.1</b> 添加分组</a></li>
<li class="chapter" data-level="2.5.2" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#查看分组"><i class="fa fa-check"></i><b>2.5.2</b> 查看分组</a></li>
<li class="chapter" data-level="2.5.3" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#更改和添加分组变量"><i class="fa fa-check"></i><b>2.5.3</b> 更改和添加分组变量</a></li>
<li class="chapter" data-level="2.5.4" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#删除分组变量"><i class="fa fa-check"></i><b>2.5.4</b> 删除分组变量</a></li>
<li class="chapter" data-level="2.5.5" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#动词影响"><i class="fa fa-check"></i><b>2.5.5</b> 动词影响</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html"><i class="fa fa-check"></i><b>2.6</b> 列操作</a>
<ul>
<li class="chapter" data-level="2.6.1" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#dplyr-column-manipulation-basic-usage"><i class="fa fa-check"></i><b>2.6.1</b> 基础用法</a></li>
<li class="chapter" data-level="2.6.2" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#多种函数功能"><i class="fa fa-check"></i><b>2.6.2</b> 多种函数功能</a></li>
<li class="chapter" data-level="2.6.3" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#当前列"><i class="fa fa-check"></i><b>2.6.3</b> 当前列</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html"><i class="fa fa-check"></i><b>2.7</b> 行操作</a>
<ul>
<li class="chapter" data-level="2.7.1" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html#比较差异"><i class="fa fa-check"></i><b>2.7.1</b> 比较差异</a></li>
<li class="chapter" data-level="2.7.2" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html#常用案例"><i class="fa fa-check"></i><b>2.7.2</b> 常用案例</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="dplyr-programming.html"><a href="dplyr-programming.html"><i class="fa fa-check"></i><b>2.8</b> <code>dplyr</code>编程</a>
<ul>
<li class="chapter" data-level="2.8.1" data-path="dplyr-programming.html"><a href="dplyr-programming.html#案例"><i class="fa fa-check"></i><b>2.8.1</b> 案例</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="dplyr-reference-material.html"><a href="dplyr-reference-material.html"><i class="fa fa-check"></i><b>2.9</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="Data-tidyr-package.html"><a href="Data-tidyr-package.html"><i class="fa fa-check"></i><b>3</b> 整洁数据之 tidyr</a>
<ul>
<li class="chapter" data-level="3.1" data-path="tidyr-useage.html"><a href="tidyr-useage.html"><i class="fa fa-check"></i><b>3.1</b> tidyr 介绍</a>
<ul>
<li class="chapter" data-level="3.1.1" data-path="tidyr-useage.html"><a href="tidyr-useage.html#tidyr:install"><i class="fa fa-check"></i><b>3.1.1</b> 安装</a></li>
<li class="chapter" data-level="3.1.2" data-path="tidyr-useage.html"><a href="tidyr-useage.html#tidyr:functions"><i class="fa fa-check"></i><b>3.1.2</b> 主要功能</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="pivoting.html"><a href="pivoting.html"><i class="fa fa-check"></i><b>3.2</b> 长宽转换</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="pivoting.html"><a href="pivoting.html#宽转长"><i class="fa fa-check"></i><b>3.2.1</b> 宽转长</a></li>
<li class="chapter" data-level="3.2.2" data-path="pivoting.html"><a href="pivoting.html#用法-1"><i class="fa fa-check"></i><b>3.2.2</b> 用法</a></li>
<li class="chapter" data-level="3.2.3" data-path="pivoting.html"><a href="pivoting.html#长转宽"><i class="fa fa-check"></i><b>3.2.3</b> 长转宽</a></li>
<li class="chapter" data-level="3.2.4" data-path="pivoting.html"><a href="pivoting.html#用法-2"><i class="fa fa-check"></i><b>3.2.4</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="tidyr-rectangle.html"><a href="tidyr-rectangle.html"><i class="fa fa-check"></i><b>3.3</b> 矩形数据</a></li>
<li class="chapter" data-level="3.4" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html"><i class="fa fa-check"></i><b>3.4</b> 嵌套数据</a>
<ul>
<li class="chapter" data-level="3.4.1" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html#基础"><i class="fa fa-check"></i><b>3.4.1</b> 基础</a></li>
<li class="chapter" data-level="3.4.2" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html#嵌套数据和模型"><i class="fa fa-check"></i><b>3.4.2</b> 嵌套数据和模型</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="character-vector.html"><a href="character-vector.html"><i class="fa fa-check"></i><b>3.5</b> 字符处理</a>
<ul>
<li class="chapter" data-level="3.5.1" data-path="character-vector.html"><a href="character-vector.html#新列提取"><i class="fa fa-check"></i><b>3.5.1</b> 新列提取</a></li>
<li class="chapter" data-level="3.5.2" data-path="character-vector.html"><a href="character-vector.html#拆分"><i class="fa fa-check"></i><b>3.5.2</b> 拆分</a></li>
<li class="chapter" data-level="3.5.3" data-path="character-vector.html"><a href="character-vector.html#列拆分行"><i class="fa fa-check"></i><b>3.5.3</b> 列拆分行</a></li>
<li class="chapter" data-level="3.5.4" data-path="character-vector.html"><a href="character-vector.html#合并多列"><i class="fa fa-check"></i><b>3.5.4</b> 合并多列</a></li>
</ul></li>
<li class="chapter" data-level="3.6" data-path="tidyr-expand.html"><a href="tidyr-expand.html"><i class="fa fa-check"></i><b>3.6</b> 扩展数据框</a></li>
<li class="chapter" data-level="3.7" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html"><i class="fa fa-check"></i><b>3.7</b> 缺失值</a>
<ul>
<li class="chapter" data-level="3.7.1" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#replace_na"><i class="fa fa-check"></i><b>3.7.1</b> replace_na</a></li>
<li class="chapter" data-level="3.7.2" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#fill"><i class="fa fa-check"></i><b>3.7.2</b> fill</a></li>
<li class="chapter" data-level="3.7.3" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#drop_na"><i class="fa fa-check"></i><b>3.7.3</b> drop_na</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="character-handling.html"><a href="character-handling.html"><i class="fa fa-check"></i><b>4</b> 字符处理</a>
<ul>
<li class="chapter" data-level="4.1" data-path="character-base-R.html"><a href="character-base-R.html"><i class="fa fa-check"></i><b>4.1</b> base R</a>
<ul>
<li class="chapter" data-level="4.1.1" data-path="character-base-R.html"><a href="character-base-R.html#单双引号"><i class="fa fa-check"></i><b>4.1.1</b> 单双引号</a></li>
<li class="chapter" data-level="4.1.2" data-path="character-base-R.html"><a href="character-base-R.html#转义"><i class="fa fa-check"></i><b>4.1.2</b> 转义</a></li>
<li class="chapter" data-level="4.1.3" data-path="character-base-R.html"><a href="character-base-R.html#常用函数"><i class="fa fa-check"></i><b>4.1.3</b> 常用函数</a></li>
<li class="chapter" data-level="4.1.4" data-path="character-base-R.html"><a href="character-base-R.html#newfeatures"><i class="fa fa-check"></i><b>4.1.4</b> 新特性</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html"><i class="fa fa-check"></i><b>4.2</b> stringr</a>
<ul>
<li class="chapter" data-level="4.2.1" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-install"><i class="fa fa-check"></i><b>4.2.1</b> 安装</a></li>
<li class="chapter" data-level="4.2.2" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-usage"><i class="fa fa-check"></i><b>4.2.2</b> 基本使用</a></li>
<li class="chapter" data-level="4.2.3" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-functions"><i class="fa fa-check"></i><b>4.2.3</b> 常用函数</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="character-application.html"><a href="character-application.html"><i class="fa fa-check"></i><b>4.3</b> 综合运用</a>
<ul>
<li class="chapter" data-level="4.3.1" data-path="character-application.html"><a href="character-application.html#实现excel函数"><i class="fa fa-check"></i><b>4.3.1</b> 实现excel函数</a></li>
<li class="chapter" data-level="4.3.2" data-path="character-application.html"><a href="character-application.html#使用案例"><i class="fa fa-check"></i><b>4.3.2</b> 使用案例</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="character-the-difference-stringr-and-base.html"><a href="character-the-difference-stringr-and-base.html"><i class="fa fa-check"></i><b>4.4</b> base 和 stringr</a></li>
<li class="chapter" data-level="4.5" data-path="character-reference-material.html"><a href="character-reference-material.html"><i class="fa fa-check"></i><b>4.5</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="datetime.html"><a href="datetime.html"><i class="fa fa-check"></i><b>5</b> 时间处理</a>
<ul>
<li class="chapter" data-level="5.1" data-path="datetime-base-R.html"><a href="datetime-base-R.html"><i class="fa fa-check"></i><b>5.1</b> base R</a>
<ul>
<li class="chapter" data-level="5.1.1" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-date-class"><i class="fa fa-check"></i><b>5.1.1</b> Date</a></li>
<li class="chapter" data-level="5.1.2" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-POSIXct-class"><i class="fa fa-check"></i><b>5.1.2</b> POSIXct</a></li>
<li class="chapter" data-level="5.1.3" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-POSIXlt-class"><i class="fa fa-check"></i><b>5.1.3</b> POSIXlt</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="lubridate.html"><a href="lubridate.html"><i class="fa fa-check"></i><b>5.2</b> lubridate</a>
<ul>
<li class="chapter" data-level="5.2.1" data-path="lubridate.html"><a href="lubridate.html#lubridate-install"><i class="fa fa-check"></i><b>5.2.1</b> 安装包</a></li>
<li class="chapter" data-level="5.2.2" data-path="lubridate.html"><a href="lubridate.html#get-current-datetime"><i class="fa fa-check"></i><b>5.2.2</b> 当前时间日期</a></li>
<li class="chapter" data-level="5.2.3" data-path="lubridate.html"><a href="lubridate.html#make-datetime"><i class="fa fa-check"></i><b>5.2.3</b> 构造日期时间</a></li>
<li class="chapter" data-level="5.2.4" data-path="lubridate.html"><a href="lubridate.html#parse-datetime"><i class="fa fa-check"></i><b>5.2.4</b> 解析日期时间</a></li>
<li class="chapter" data-level="5.2.5" data-path="lubridate.html"><a href="lubridate.html#extracting-datetime-information"><i class="fa fa-check"></i><b>5.2.5</b> 提取日期时间成分</a></li>
<li class="chapter" data-level="5.2.6" data-path="lubridate.html"><a href="lubridate.html#time-zones"><i class="fa fa-check"></i><b>5.2.6</b> 处理时区</a></li>
<li class="chapter" data-level="5.2.7" data-path="lubridate.html"><a href="lubridate.html#interveal"><i class="fa fa-check"></i><b>5.2.7</b> 时间间隔</a></li>
<li class="chapter" data-level="5.2.8" data-path="lubridate.html"><a href="lubridate.html#calculate-datetime"><i class="fa fa-check"></i><b>5.2.8</b> 时间日期计算</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="datetime-application.html"><a href="datetime-application.html"><i class="fa fa-check"></i><b>5.3</b> 综合运用</a>
<ul>
<li class="chapter" data-level="5.3.1" data-path="datetime-application.html"><a href="datetime-application.html#日报同环比计算"><i class="fa fa-check"></i><b>5.3.1</b> 日报同环比计算</a></li>
<li class="chapter" data-level="5.3.2" data-path="datetime-application.html"><a href="datetime-application.html#清洗不同类型日期格式"><i class="fa fa-check"></i><b>5.3.2</b> 清洗不同类型日期格式</a></li>
<li class="chapter" data-level="5.3.3" data-path="datetime-application.html"><a href="datetime-application.html#扫码后中奖时间匹配"><i class="fa fa-check"></i><b>5.3.3</b> 扫码后中奖时间匹配</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html"><i class="fa fa-check"></i><b>5.4</b> 补充资料</a>
<ul>
<li class="chapter" data-level="5.4.1" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html#excel-and-r"><i class="fa fa-check"></i><b>5.4.1</b> Excel and R</a></li>
<li class="chapter" data-level="5.4.2" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html#参考资料-1"><i class="fa fa-check"></i><b>5.4.2</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="forcats.html"><a href="forcats.html"><i class="fa fa-check"></i><b>6</b> 因子介绍</a>
<ul>
<li class="chapter" data-level="6.1" data-path="forcats-introduction.html"><a href="forcats-introduction.html"><i class="fa fa-check"></i><b>6.1</b> 介绍</a>
<ul>
<li class="chapter" data-level="6.1.1" data-path="forcats-introduction.html"><a href="forcats-introduction.html#forcats:install"><i class="fa fa-check"></i><b>6.1.1</b> 安装</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="forcats-usage.html"><a href="forcats-usage.html"><i class="fa fa-check"></i><b>6.2</b> 用法</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="forcats-usage.html"><a href="forcats-usage.html#创建因子"><i class="fa fa-check"></i><b>6.2.1</b> 创建因子</a></li>
<li class="chapter" data-level="6.2.2" data-path="forcats-usage.html"><a href="forcats-usage.html#修改因子顺序"><i class="fa fa-check"></i><b>6.2.2</b> 修改因子顺序</a></li>
<li class="chapter" data-level="6.2.3" data-path="forcats-usage.html"><a href="forcats-usage.html#修改因子级别"><i class="fa fa-check"></i><b>6.2.3</b> 修改因子级别</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="7" data-path="Data-Manipulation-datatable.html"><a href="Data-Manipulation-datatable.html"><i class="fa fa-check"></i><b>7</b> data.table</a>
<ul>
<li class="chapter" data-level="7.1" data-path="datatable-description.html"><a href="datatable-description.html"><i class="fa fa-check"></i><b>7.1</b> 基础介绍</a>
<ul>
<li class="chapter" data-level="7.1.1" data-path="datatable-description.html"><a href="datatable-description.html#读取数据"><i class="fa fa-check"></i><b>7.1.1</b> 读取数据</a></li>
<li class="chapter" data-level="7.1.2" data-path="datatable-description.html"><a href="datatable-description.html#基本格式"><i class="fa fa-check"></i><b>7.1.2</b> 基本格式</a></li>
<li class="chapter" data-level="7.1.3" data-path="datatable-description.html"><a href="datatable-description.html#i-j-by-使用"><i class="fa fa-check"></i><b>7.1.3</b> i j by 使用</a></li>
<li class="chapter" data-level="7.1.4" data-path="datatable-description.html"><a href="datatable-description.html#行列筛选总结"><i class="fa fa-check"></i><b>7.1.4</b> 行列筛选总结</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="datatable-usage.html"><a href="datatable-usage.html"><i class="fa fa-check"></i><b>7.2</b> 常规操作</a>
<ul>
<li class="chapter" data-level="7.2.1" data-path="datatable-usage.html"><a href="datatable-usage.html#行筛选"><i class="fa fa-check"></i><b>7.2.1</b> 行筛选</a></li>
<li class="chapter" data-level="7.2.2" data-path="datatable-usage.html"><a href="datatable-usage.html#新增更新列"><i class="fa fa-check"></i><b>7.2.2</b> 新增更新列</a></li>
<li class="chapter" data-level="7.2.3" data-path="datatable-usage.html"><a href="datatable-usage.html#排序"><i class="fa fa-check"></i><b>7.2.3</b> 排序</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="datatable-function.html"><a href="datatable-function.html"><i class="fa fa-check"></i><b>7.3</b> 常用函数</a>
<ul>
<li class="chapter" data-level="7.3.1" data-path="datatable-function.html"><a href="datatable-function.html#特殊符号"><i class="fa fa-check"></i><b>7.3.1</b> 特殊符号</a></li>
<li class="chapter" data-level="7.3.2" data-path="datatable-function.html"><a href="datatable-function.html#排序函数-1"><i class="fa fa-check"></i><b>7.3.2</b> 排序函数</a></li>
<li class="chapter" data-level="7.3.3" data-path="datatable-function.html"><a href="datatable-function.html#非重复计数"><i class="fa fa-check"></i><b>7.3.3</b> 非重复计数</a></li>
<li class="chapter" data-level="7.3.4" data-path="datatable-function.html"><a href="datatable-function.html#判断函数"><i class="fa fa-check"></i><b>7.3.4</b> 判断函数</a></li>
<li class="chapter" data-level="7.3.5" data-path="datatable-function.html"><a href="datatable-function.html#交集-差集-合并"><i class="fa fa-check"></i><b>7.3.5</b> 交集 差集 合并</a></li>
<li class="chapter" data-level="7.3.6" data-path="datatable-function.html"><a href="datatable-function.html#长宽转换"><i class="fa fa-check"></i><b>7.3.6</b> 长宽转换</a></li>
<li class="chapter" data-level="7.3.7" data-path="datatable-function.html"><a href="datatable-function.html#表连接"><i class="fa fa-check"></i><b>7.3.7</b> 表连接</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html"><i class="fa fa-check"></i><b>7.4</b> 高级函数</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#groupingsets"><i class="fa fa-check"></i><b>7.4.1</b> groupingsets</a></li>
<li class="chapter" data-level="7.4.2" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#rleid"><i class="fa fa-check"></i><b>7.4.2</b> rleid</a></li>
<li class="chapter" data-level="7.4.3" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#shift"><i class="fa fa-check"></i><b>7.4.3</b> shift</a></li>
<li class="chapter" data-level="7.4.4" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#j"><i class="fa fa-check"></i><b>7.4.4</b> J</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="datatable-tips.html"><a href="datatable-tips.html"><i class="fa fa-check"></i><b>7.5</b> 小技巧</a>
<ul>
<li class="chapter" data-level="7.5.1" data-path="datatable-tips.html"><a href="datatable-tips.html#用抑制中间过程输出"><i class="fa fa-check"></i><b>7.5.1</b> 用{}抑制中间过程输出</a></li>
<li class="chapter" data-level="7.5.2" data-path="datatable-tips.html"><a href="datatable-tips.html#使用打印data.table"><i class="fa fa-check"></i><b>7.5.2</b> 使用[]打印data.table</a></li>
</ul></li>
<li class="chapter" data-level="7.6" data-path="datatable-application.html"><a href="datatable-application.html"><i class="fa fa-check"></i><b>7.6</b> 运用</a>
<ul>
<li class="chapter" data-level="7.6.1" data-path="datatable-application.html"><a href="datatable-application.html#自定义函数计算"><i class="fa fa-check"></i><b>7.6.1</b> 自定义函数计算</a></li>
<li class="chapter" data-level="7.6.2" data-path="datatable-application.html"><a href="datatable-application.html#带汇总的聚合运算"><i class="fa fa-check"></i><b>7.6.2</b> 带汇总的聚合运算</a></li>
<li class="chapter" data-level="7.6.3" data-path="datatable-application.html"><a href="datatable-application.html#行列转变"><i class="fa fa-check"></i><b>7.6.3</b> 行列转变</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="8" data-path="database.html"><a href="database.html"><i class="fa fa-check"></i><b>8</b> database</a>
<ul>
<li class="chapter" data-level="8.1" data-path="database-install.html"><a href="database-install.html"><i class="fa fa-check"></i><b>8.1</b> 安装数据库</a></li>
<li class="chapter" data-level="8.2" data-path="database-odbc-package.html"><a href="database-odbc-package.html"><i class="fa fa-check"></i><b>8.2</b> odbc包</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="database-odbc-package.html"><a href="database-odbc-package.html#安装包"><i class="fa fa-check"></i><b>8.2.1</b> 安装包</a></li>
<li class="chapter" data-level="8.2.2" data-path="database-odbc-package.html"><a href="database-odbc-package.html#驱动安装"><i class="fa fa-check"></i><b>8.2.2</b> 驱动安装</a></li>
<li class="chapter" data-level="8.2.3" data-path="database-odbc-package.html"><a href="database-odbc-package.html#连接数据库"><i class="fa fa-check"></i><b>8.2.3</b> 连接数据库</a></li>
<li class="chapter" data-level="8.2.4" data-path="database-odbc-package.html"><a href="database-odbc-package.html#读写数据库"><i class="fa fa-check"></i><b>8.2.4</b> 读写数据库</a></li>
</ul></li>
<li class="chapter" data-level="8.3" data-path="database-DBI-package.html"><a href="database-DBI-package.html"><i class="fa fa-check"></i><b>8.3</b> DBI包</a>
<ul>
<li class="chapter" data-level="8.3.1" data-path="database-DBI-package.html"><a href="database-DBI-package.html#安装-1"><i class="fa fa-check"></i><b>8.3.1</b> 安装</a></li>
<li class="chapter" data-level="8.3.2" data-path="database-DBI-package.html"><a href="database-DBI-package.html#连接数据库-1"><i class="fa fa-check"></i><b>8.3.2</b> 连接数据库</a></li>
<li class="chapter" data-level="8.3.3" data-path="database-DBI-package.html"><a href="database-DBI-package.html#读写数据库-1"><i class="fa fa-check"></i><b>8.3.3</b> 读写数据库</a></li>
<li class="chapter" data-level="8.3.4" data-path="database-DBI-package.html"><a href="database-DBI-package.html#其它函数介绍"><i class="fa fa-check"></i><b>8.3.4</b> 其它函数介绍</a></li>
</ul></li>
<li class="chapter" data-level="8.4" data-path="database-else-package.html"><a href="database-else-package.html"><i class="fa fa-check"></i><b>8.4</b> 其他R包</a>
<ul>
<li class="chapter" data-level="8.4.1" data-path="database-else-package.html"><a href="database-else-package.html#rodbc包"><i class="fa fa-check"></i><b>8.4.1</b> RODBC包</a></li>
<li class="chapter" data-level="8.4.2" data-path="database-else-package.html"><a href="database-else-package.html#roracle包"><i class="fa fa-check"></i><b>8.4.2</b> ROracle包</a></li>
<li class="chapter" data-level="8.4.3" data-path="database-else-package.html"><a href="database-else-package.html#rmysql包"><i class="fa fa-check"></i><b>8.4.3</b> RMySQL包</a></li>
<li class="chapter" data-level="8.4.4" data-path="database-else-package.html"><a href="database-else-package.html#rsqlite"><i class="fa fa-check"></i><b>8.4.4</b> RSQLite</a></li>
</ul></li>
<li class="chapter" data-level="8.5" data-path="database-big-data.html"><a href="database-big-data.html"><i class="fa fa-check"></i><b>8.5</b> 大数据相关R包</a>
<ul>
<li class="chapter" data-level="8.5.1" data-path="database-big-data.html"><a href="database-big-data.html#sparklyr"><i class="fa fa-check"></i><b>8.5.1</b> sparklyr</a></li>
<li class="chapter" data-level="8.5.2" data-path="database-big-data.html"><a href="database-big-data.html#RClickhouse"><i class="fa fa-check"></i><b>8.5.2</b> RClickhouse</a></li>
</ul></li>
<li class="chapter" data-level="8.6" data-path="database-FAQ.html"><a href="database-FAQ.html"><i class="fa fa-check"></i><b>8.6</b> 常见问题</a>
<ul>
<li class="chapter" data-level="8.6.1" data-path="database-FAQ.html"><a href="database-FAQ.html#乱码问题"><i class="fa fa-check"></i><b>8.6.1</b> 乱码问题</a></li>
<li class="chapter" data-level="8.6.2" data-path="database-FAQ.html"><a href="database-FAQ.html#无法连接问题"><i class="fa fa-check"></i><b>8.6.2</b> 无法连接问题</a></li>
<li class="chapter" data-level="8.6.3" data-path="database-FAQ.html"><a href="database-FAQ.html#远程连接"><i class="fa fa-check"></i><b>8.6.3</b> 远程连接</a></li>
<li class="chapter" data-level="8.6.4" data-path="database-FAQ.html"><a href="database-FAQ.html#跳板机连接"><i class="fa fa-check"></i><b>8.6.4</b> 跳板机连接</a></li>
</ul></li>
<li class="chapter" data-level="8.7" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html"><i class="fa fa-check"></i><b>8.7</b> dbplyr</a>
<ul>
<li class="chapter" data-level="8.7.1" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#安装-2"><i class="fa fa-check"></i><b>8.7.1</b> 安装</a></li>
<li class="chapter" data-level="8.7.2" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#基础用法-1"><i class="fa fa-check"></i><b>8.7.2</b> 基础用法</a></li>
<li class="chapter" data-level="8.7.3" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#无法正确转化"><i class="fa fa-check"></i><b>8.7.3</b> 无法正确转化</a></li>
</ul></li>
<li class="chapter" data-level="8.8" data-path="database-reference-material.html"><a href="database-reference-material.html"><i class="fa fa-check"></i><b>8.8</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="R-loop-structure.html"><a href="R-loop-structure.html"><i class="fa fa-check"></i><b>9</b> 循环结构</a>
<ul>
<li class="chapter" data-level="9.1" data-path="R-loop-description.html"><a href="R-loop-description.html"><i class="fa fa-check"></i><b>9.1</b> R循环介绍</a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="R-loop-description.html"><a href="R-loop-description.html#R:loop-example"><i class="fa fa-check"></i><b>9.1.1</b> 简单示例</a></li>
<li class="chapter" data-level="9.1.2" data-path="R-loop-description.html"><a href="R-loop-description.html#three:loop-structure"><i class="fa fa-check"></i><b>9.1.2</b> 循环结构</a></li>
<li class="chapter" data-level="9.1.3" data-path="R-loop-description.html"><a href="R-loop-description.html#loop:control"><i class="fa fa-check"></i><b>9.1.3</b> 循环控制</a></li>
<li class="chapter" data-level="9.1.4" data-path="R-loop-description.html"><a href="R-loop-description.html#嵌套循环"><i class="fa fa-check"></i><b>9.1.4</b> 嵌套循环</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="loop-changes.html"><a href="loop-changes.html"><i class="fa fa-check"></i><b>9.2</b> 循环变化</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="loop-changes.html"><a href="loop-changes.html#修改已有对象"><i class="fa fa-check"></i><b>9.2.1</b> 修改已有对象</a></li>
<li class="chapter" data-level="9.2.2" data-path="loop-changes.html"><a href="loop-changes.html#循环模式"><i class="fa fa-check"></i><b>9.2.2</b> 循环模式</a></li>
<li class="chapter" data-level="9.2.3" data-path="loop-changes.html"><a href="loop-changes.html#未知长度输出"><i class="fa fa-check"></i><b>9.2.3</b> 未知长度输出</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="apply-function.html"><a href="apply-function.html"><i class="fa fa-check"></i><b>9.3</b> apply系列函数</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="apply-function.html"><a href="apply-function.html#lapply"><i class="fa fa-check"></i><b>9.3.1</b> lapply 循环迭代</a></li>
<li class="chapter" data-level="9.3.2" data-path="apply-function.html"><a href="apply-function.html#sapply"><i class="fa fa-check"></i><b>9.3.2</b> sapply</a></li>
<li class="chapter" data-level="9.3.3" data-path="apply-function.html"><a href="apply-function.html#vapply"><i class="fa fa-check"></i><b>9.3.3</b> vapply 迭代安全函数</a></li>
<li class="chapter" data-level="9.3.4" data-path="apply-function.html"><a href="apply-function.html#apply"><i class="fa fa-check"></i><b>9.3.4</b> apply 多维数据</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="purrr-Iteration.html"><a href="purrr-Iteration.html"><i class="fa fa-check"></i><b>10</b> 循环迭代purrr介绍</a>
<ul>
<li class="chapter" data-level="10.1" data-path="安装-3.html"><a href="安装-3.html"><i class="fa fa-check"></i><b>10.1</b> 安装</a></li>
<li class="chapter" data-level="10.2" data-path="purrr-basic-usage.html"><a href="purrr-basic-usage.html"><i class="fa fa-check"></i><b>10.2</b> 常规用法</a>
<ul>
<li class="chapter" data-level="10.2.1" data-path="purrr-basic-usage.html"><a href="purrr-basic-usage.html#map-系列"><i class="fa fa-check"></i><b>10.2.1</b> map 系列</a></li>
<li class="chapter" data-level="10.2.2" data-path="purrr-basic-usage.html"><a href="purrr-basic-usage.html#pmap-系列"><i class="fa fa-check"></i><b>10.2.2</b> pmap 系列</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="purrr-map.html"><a href="purrr-map.html"><i class="fa fa-check"></i><b>10.3</b> map系列函数</a>
<ul>
<li class="chapter" data-level="10.3.1" data-path="purrr-map.html"><a href="purrr-map.html#用法-3"><i class="fa fa-check"></i><b>10.3.1</b> 用法</a></li>
<li class="chapter" data-level="10.3.2" data-path="purrr-map.html"><a href="purrr-map.html#案例-5"><i class="fa fa-check"></i><b>10.3.2</b> 案例</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="purrr-map2.html"><a href="purrr-map2.html"><i class="fa fa-check"></i><b>10.4</b> map2 系列函数</a></li>
<li class="chapter" data-level="10.5" data-path="purrr-multi-parameter.html"><a href="purrr-multi-parameter.html"><i class="fa fa-check"></i><b>10.5</b> pmap系列函数</a></li>
<li class="chapter" data-level="10.6" data-path="purrr-reduce.html"><a href="purrr-reduce.html"><i class="fa fa-check"></i><b>10.6</b> 归约累计函数</a>
<ul>
<li class="chapter" data-level="10.6.1" data-path="purrr-reduce.html"><a href="purrr-reduce.html#reduce-函数"><i class="fa fa-check"></i><b>10.6.1</b> reduce 函数</a></li>
<li class="chapter" data-level="10.6.2" data-path="purrr-reduce.html"><a href="purrr-reduce.html#accumulate-函数"><i class="fa fa-check"></i><b>10.6.2</b> accumulate 函数</a></li>
</ul></li>
<li class="chapter" data-level="10.7" data-path="purrr-safely-functions.html"><a href="purrr-safely-functions.html"><i class="fa fa-check"></i><b>10.7</b> 安全函数</a></li>
<li class="chapter" data-level="10.8" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html"><i class="fa fa-check"></i><b>10.8</b> 其他函数介绍</a>
<ul>
<li class="chapter" data-level="10.8.1" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#flatten-系列"><i class="fa fa-check"></i><b>10.8.1</b> flatten 系列</a></li>
<li class="chapter" data-level="10.8.2" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#imap-函数"><i class="fa fa-check"></i><b>10.8.2</b> imap 函数</a></li>
<li class="chapter" data-level="10.8.3" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#walk-系列"><i class="fa fa-check"></i><b>10.8.3</b> walk 系列</a></li>
<li class="chapter" data-level="10.8.4" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#逻辑判断函数"><i class="fa fa-check"></i><b>10.8.4</b> 逻辑判断函数</a></li>
<li class="chapter" data-level="10.8.5" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#提取函数"><i class="fa fa-check"></i><b>10.8.5</b> 提取函数</a></li>
<li class="chapter" data-level="10.8.6" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#筛选函数"><i class="fa fa-check"></i><b>10.8.6</b> 筛选函数</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="11" data-path="self-define-function.html"><a href="self-define-function.html"><i class="fa fa-check"></i><b>11</b> 自定义函数</a>
<ul>
<li class="chapter" data-level="11.1" data-path="define-function-example.html"><a href="define-function-example.html"><i class="fa fa-check"></i><b>11.1</b> 简单示例</a></li>
<li class="chapter" data-level="11.2" data-path="define-function-conditional-execution.html"><a href="define-function-conditional-execution.html"><i class="fa fa-check"></i><b>11.2</b> 条件执行</a>
<ul>
<li class="chapter" data-level="11.2.1" data-path="define-function-conditional-execution.html"><a href="define-function-conditional-execution.html#多条件执行"><i class="fa fa-check"></i><b>11.2.1</b> 多条件执行</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="define-function-parameter.html"><a href="define-function-parameter.html"><i class="fa fa-check"></i><b>11.3</b> 函数参数</a>
<ul>
<li class="chapter" data-level="11.3.1" data-path="define-function-parameter.html"><a href="define-function-parameter.html#参数名称"><i class="fa fa-check"></i><b>11.3.1</b> 参数名称</a></li>
<li class="chapter" data-level="11.3.2" data-path="define-function-parameter.html"><a href="define-function-parameter.html#检查参数值"><i class="fa fa-check"></i><b>11.3.2</b> 检查参数值</a></li>
<li class="chapter" data-level="11.3.3" data-path="define-function-parameter.html"><a href="define-function-parameter.html#参数-5"><i class="fa fa-check"></i><b>11.3.3</b> …参数</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="function-return.html"><a href="function-return.html"><i class="fa fa-check"></i><b>11.4</b> 返回值</a>
<ul>
<li class="chapter" data-level="11.4.1" data-path="function-return.html"><a href="function-return.html#显式返回"><i class="fa fa-check"></i><b>11.4.1</b> 显式返回</a></li>
<li class="chapter" data-level="11.4.2" data-path="function-return.html"><a href="function-return.html#编写管道函数"><i class="fa fa-check"></i><b>11.4.2</b> 编写管道函数</a></li>
</ul></li>
<li class="chapter" data-level="11.5" data-path="function-environment.html"><a href="function-environment.html"><i class="fa fa-check"></i><b>11.5</b> 环境</a></li>
<li class="chapter" data-level="11.6" data-path="function-expand.html"><a href="function-expand.html"><i class="fa fa-check"></i><b>11.6</b> 拓展部分</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="file-system-operation.html"><a href="file-system-operation.html"><i class="fa fa-check"></i><b>12</b> R语言文件系统操作</a>
<ul>
<li class="chapter" data-level="12.1" data-path="file-base-r.html"><a href="file-base-r.html"><i class="fa fa-check"></i><b>12.1</b> base R</a>
<ul>
<li class="chapter" data-level="12.1.1" data-path="file-base-r.html"><a href="file-base-r.html#base:r-usage"><i class="fa fa-check"></i><b>12.1.1</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="12.2" data-path="fs-package.html"><a href="fs-package.html"><i class="fa fa-check"></i><b>12.2</b> fs package</a>
<ul>
<li class="chapter" data-level="12.2.1" data-path="fs-package.html"><a href="fs-package.html#fs-install"><i class="fa fa-check"></i><b>12.2.1</b> 安装</a></li>
<li class="chapter" data-level="12.2.2" data-path="fs-package.html"><a href="fs-package.html#fs-advanced"><i class="fa fa-check"></i><b>12.2.2</b> 优势</a></li>
<li class="chapter" data-level="12.2.3" data-path="fs-package.html"><a href="fs-package.html#fs-usage"><i class="fa fa-check"></i><b>12.2.3</b> 基础用法</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="fs-functions.html"><a href="fs-functions.html"><i class="fa fa-check"></i><b>12.3</b> fs函数介绍</a>
<ul>
<li class="chapter" data-level="12.3.1" data-path="fs-functions.html"><a href="fs-functions.html#fs-file-manipulation"><i class="fa fa-check"></i><b>12.3.1</b> 文件操作</a></li>
<li class="chapter" data-level="12.3.2" data-path="fs-functions.html"><a href="fs-functions.html#fs-dir-manipulation"><i class="fa fa-check"></i><b>12.3.2</b> 目录操作</a></li>
<li class="chapter" data-level="12.3.3" data-path="fs-functions.html"><a href="fs-functions.html#fs-link-manipulation"><i class="fa fa-check"></i><b>12.3.3</b> 链接操作</a></li>
<li class="chapter" data-level="12.3.4" data-path="fs-functions.html"><a href="fs-functions.html#fs-path-manipulation"><i class="fa fa-check"></i><b>12.3.4</b> 路径操作</a></li>
<li class="chapter" data-level="12.3.5" data-path="fs-functions.html"><a href="fs-functions.html#fs-help-manipulation"><i class="fa fa-check"></i><b>12.3.5</b> 辅助函数</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="file-base-fs-difference.html"><a href="file-base-fs-difference.html"><i class="fa fa-check"></i><b>12.4</b> base-fs-shell比较</a></li>
<li class="chapter" data-level="12.5" data-path="file-manipulation-reference-material.html"><a href="file-manipulation-reference-material.html"><i class="fa fa-check"></i><b>12.5</b> 参考资料</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://gitee.com/zhongyufei/Rbook" target="blank">gitee地址</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">商业数据分析师-R 语言数据处理</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="dplyr-functions" class="section level2" number="2.3">
<h2><span class="header-section-number">2.3</span> 常用函数</h2>
<p>本小节介绍dplyr处理数据时常用的函数，类似sql中的case_when、count、distinct、row_number、between等函数。</p>
<div id="条件判断" class="section level3" number="2.3.1">
<h3><span class="header-section-number">2.3.1</span> 条件判断</h3>
<p>dplyr::<code>if_else()</code>相比于<code>base::ifelse</code>,<code>if_else</code>输出类型更为严格，无论<code>TRUE</code>或<code>FALSE</code>输出类型一致，这样计算速度更快。</p>
<p>dplyr::<code>if_else()</code>参数:</p>
<div class="sourceCode" id="cb98"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb98-1"><a href="dplyr-functions.html#cb98-1" aria-hidden="true" tabindex="-1"></a><span class="fu">if_else</span>(condition, true, false, <span class="at">missing =</span> <span class="cn">NULL</span>)</span></code></pre></div>
<div class="sourceCode" id="cb99"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb99-1"><a href="dplyr-functions.html#cb99-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,<span class="dv">5</span>)</span>
<span id="cb99-2"><a href="dplyr-functions.html#cb99-2" aria-hidden="true" tabindex="-1"></a><span class="fu">if_else</span>(x<span class="sc">&gt;</span><span class="dv">5</span>,<span class="st">&quot;大于五&quot;</span>,<span class="st">&quot;小于等于五&quot;</span>,<span class="at">missing =</span> <span class="st">&quot;空值&quot;</span>)</span>
<span id="cb99-3"><a href="dplyr-functions.html#cb99-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;大于五&quot;     &quot;大于五&quot;     &quot;大于五&quot;     &quot;大于五&quot;     &quot;小于等于五&quot;</span></span></code></pre></div>
<p>与<code>ifelse</code>不同的是，<code>if_else</code>保留类型</p>
<div class="sourceCode" id="cb100"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb100-1"><a href="dplyr-functions.html#cb100-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">factor</span>(<span class="fu">sample</span>(letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>], <span class="dv">10</span>, <span class="at">replace =</span> <span class="cn">TRUE</span>))</span>
<span id="cb100-2"><a href="dplyr-functions.html#cb100-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ifelse</span>(x <span class="sc">%in%</span> <span class="fu">c</span>(<span class="st">&quot;a&quot;</span>, <span class="st">&quot;b&quot;</span>, <span class="st">&quot;c&quot;</span>), x, <span class="fu">factor</span>(<span class="cn">NA</span>))</span>
<span id="cb100-3"><a href="dplyr-functions.html#cb100-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [1] NA  2  1 NA  1  2  3  1  1  2</span></span>
<span id="cb100-4"><a href="dplyr-functions.html#cb100-4" aria-hidden="true" tabindex="-1"></a><span class="fu">if_else</span>(x <span class="sc">%in%</span> <span class="fu">c</span>(<span class="st">&quot;a&quot;</span>, <span class="st">&quot;b&quot;</span>, <span class="st">&quot;c&quot;</span>), x, <span class="fu">factor</span>(<span class="cn">NA</span>))</span>
<span id="cb100-5"><a href="dplyr-functions.html#cb100-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [1] &lt;NA&gt; b    a    &lt;NA&gt; a    b    c    a    a    b   </span></span>
<span id="cb100-6"><a href="dplyr-functions.html#cb100-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; Levels: a b c d e</span></span></code></pre></div>
<blockquote>
<p>与<code>data.table::fifelse()</code>功能相似</p>
</blockquote>
</div>
<div id="case_when" class="section level3" number="2.3.2">
<h3><span class="header-section-number">2.3.2</span> case_when</h3>
<p>当条件嵌套较多时，使用<code>case_when</code>,使代码可读并且不易出错。与sql 中的case when 等价。</p>
<div class="sourceCode" id="cb101"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb101-1"><a href="dplyr-functions.html#cb101-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">50</span></span>
<span id="cb101-2"><a href="dplyr-functions.html#cb101-2" aria-hidden="true" tabindex="-1"></a><span class="fu">case_when</span>(</span>
<span id="cb101-3"><a href="dplyr-functions.html#cb101-3" aria-hidden="true" tabindex="-1"></a>  x <span class="sc">%%</span> <span class="dv">35</span> <span class="sc">==</span> <span class="dv">0</span> <span class="sc">~</span> <span class="st">&quot;fizz buzz&quot;</span>,</span>
<span id="cb101-4"><a href="dplyr-functions.html#cb101-4" aria-hidden="true" tabindex="-1"></a>  x <span class="sc">%%</span> <span class="dv">5</span> <span class="sc">==</span> <span class="dv">0</span> <span class="sc">~</span> <span class="st">&quot;fizz&quot;</span>,</span>
<span id="cb101-5"><a href="dplyr-functions.html#cb101-5" aria-hidden="true" tabindex="-1"></a>  x <span class="sc">%%</span> <span class="dv">7</span> <span class="sc">==</span> <span class="dv">0</span> <span class="sc">~</span> <span class="st">&quot;buzz&quot;</span>,</span>
<span id="cb101-6"><a href="dplyr-functions.html#cb101-6" aria-hidden="true" tabindex="-1"></a>  <span class="cn">TRUE</span> <span class="sc">~</span> <span class="fu">as.character</span>(x)</span>
<span id="cb101-7"><a href="dplyr-functions.html#cb101-7" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb101-8"><a href="dplyr-functions.html#cb101-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [1] &quot;1&quot;         &quot;2&quot;         &quot;3&quot;         &quot;4&quot;         &quot;fizz&quot;      &quot;6&quot;        </span></span>
<span id="cb101-9"><a href="dplyr-functions.html#cb101-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [7] &quot;buzz&quot;      &quot;8&quot;         &quot;9&quot;         &quot;fizz&quot;      &quot;11&quot;        &quot;12&quot;       </span></span>
<span id="cb101-10"><a href="dplyr-functions.html#cb101-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [13] &quot;13&quot;        &quot;buzz&quot;      &quot;fizz&quot;      &quot;16&quot;        &quot;17&quot;        &quot;18&quot;       </span></span>
<span id="cb101-11"><a href="dplyr-functions.html#cb101-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [19] &quot;19&quot;        &quot;fizz&quot;      &quot;buzz&quot;      &quot;22&quot;        &quot;23&quot;        &quot;24&quot;       </span></span>
<span id="cb101-12"><a href="dplyr-functions.html#cb101-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [25] &quot;fizz&quot;      &quot;26&quot;        &quot;27&quot;        &quot;buzz&quot;      &quot;29&quot;        &quot;fizz&quot;     </span></span>
<span id="cb101-13"><a href="dplyr-functions.html#cb101-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [31] &quot;31&quot;        &quot;32&quot;        &quot;33&quot;        &quot;34&quot;        &quot;fizz buzz&quot; &quot;36&quot;       </span></span>
<span id="cb101-14"><a href="dplyr-functions.html#cb101-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [37] &quot;37&quot;        &quot;38&quot;        &quot;39&quot;        &quot;fizz&quot;      &quot;41&quot;        &quot;buzz&quot;     </span></span>
<span id="cb101-15"><a href="dplyr-functions.html#cb101-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [43] &quot;43&quot;        &quot;44&quot;        &quot;fizz&quot;      &quot;46&quot;        &quot;47&quot;        &quot;48&quot;       </span></span>
<span id="cb101-16"><a href="dplyr-functions.html#cb101-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [49] &quot;buzz&quot;      &quot;fizz&quot;</span></span></code></pre></div>
<div class="sourceCode" id="cb102"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb102-1"><a href="dplyr-functions.html#cb102-1" aria-hidden="true" tabindex="-1"></a>Dates <span class="ot">&lt;-</span> <span class="fu">as.Date</span>(<span class="fu">c</span>(<span class="st">&#39;2018-10-01&#39;</span>, <span class="st">&#39;2018-10-02&#39;</span>, <span class="st">&#39;2018-10-03&#39;</span>,<span class="st">&#39;2018-10-04&#39;</span>))</span>
<span id="cb102-2"><a href="dplyr-functions.html#cb102-2" aria-hidden="true" tabindex="-1"></a><span class="fu">case_when</span>(</span>
<span id="cb102-3"><a href="dplyr-functions.html#cb102-3" aria-hidden="true" tabindex="-1"></a>  Dates <span class="sc">==</span> <span class="st">&#39;2018-10-01&#39;</span> <span class="sc">~</span> Dates <span class="sc">-</span> <span class="dv">1</span>,</span>
<span id="cb102-4"><a href="dplyr-functions.html#cb102-4" aria-hidden="true" tabindex="-1"></a>  Dates <span class="sc">==</span> <span class="st">&#39;2018-10-02&#39;</span> <span class="sc">~</span> Dates <span class="sc">+</span> <span class="dv">1</span>,</span>
<span id="cb102-5"><a href="dplyr-functions.html#cb102-5" aria-hidden="true" tabindex="-1"></a>  Dates <span class="sc">==</span> <span class="st">&#39;2018-10-03&#39;</span> <span class="sc">~</span> Dates <span class="sc">+</span> <span class="dv">2</span>,</span>
<span id="cb102-6"><a href="dplyr-functions.html#cb102-6" aria-hidden="true" tabindex="-1"></a>  <span class="cn">TRUE</span> <span class="sc">~</span> Dates</span>
<span id="cb102-7"><a href="dplyr-functions.html#cb102-7" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb102-8"><a href="dplyr-functions.html#cb102-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;2018-09-30&quot; &quot;2018-10-03&quot; &quot;2018-10-05&quot; &quot;2018-10-04&quot;</span></span></code></pre></div>
<p>结合 mutate　动词使用</p>
<div class="sourceCode" id="cb103"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb103-1"><a href="dplyr-functions.html#cb103-1" aria-hidden="true" tabindex="-1"></a>starwars <span class="sc">%&gt;%</span> </span>
<span id="cb103-2"><a href="dplyr-functions.html#cb103-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">mutate</span>(性别 <span class="ot">=</span> <span class="fu">case_when</span>(</span>
<span id="cb103-3"><a href="dplyr-functions.html#cb103-3" aria-hidden="true" tabindex="-1"></a>    sex <span class="sc">==</span> <span class="st">&quot;male&quot;</span> <span class="sc">~</span> <span class="st">&quot;雄性&quot;</span>,</span>
<span id="cb103-4"><a href="dplyr-functions.html#cb103-4" aria-hidden="true" tabindex="-1"></a>    sex <span class="sc">==</span> <span class="st">&quot;female&quot;</span> <span class="sc">~</span> <span class="st">&quot;雌性&quot;</span>,</span>
<span id="cb103-5"><a href="dplyr-functions.html#cb103-5" aria-hidden="true" tabindex="-1"></a>    sex <span class="sc">==</span> <span class="st">&quot;hermaphroditic&quot;</span> <span class="sc">~</span> <span class="st">&quot;雌雄同体&quot;</span>,</span>
<span id="cb103-6"><a href="dplyr-functions.html#cb103-6" aria-hidden="true" tabindex="-1"></a>    <span class="cn">TRUE</span> <span class="sc">~</span> <span class="st">&quot;无&quot;</span></span>
<span id="cb103-7"><a href="dplyr-functions.html#cb103-7" aria-hidden="true" tabindex="-1"></a>    )) <span class="sc">%&gt;%</span> </span>
<span id="cb103-8"><a href="dplyr-functions.html#cb103-8" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pull</span>(性别) <span class="sc">%&gt;%</span> </span>
<span id="cb103-9"><a href="dplyr-functions.html#cb103-9" aria-hidden="true" tabindex="-1"></a>  <span class="fu">table</span>()</span>
<span id="cb103-10"><a href="dplyr-functions.html#cb103-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; .</span></span>
<span id="cb103-11"><a href="dplyr-functions.html#cb103-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     雌性 雌雄同体       无     雄性 </span></span>
<span id="cb103-12"><a href="dplyr-functions.html#cb103-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;       16        1       10       60</span></span></code></pre></div>
<blockquote>
<p>pull()功能与　data$．类似，为了在管道中使用设计</p>
</blockquote>
</div>
<div id="计数函数" class="section level3" number="2.3.3">
<h3><span class="header-section-number">2.3.3</span> 计数函数</h3>
<ul>
<li>计数</li>
</ul>
<p><code>count()</code>函数用来计数。下面两种表达方式等价。</p>
<div class="sourceCode" id="cb104"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb104-1"><a href="dplyr-functions.html#cb104-1" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%&gt;%</span> <span class="fu">count</span>(a, b)</span>
<span id="cb104-2"><a href="dplyr-functions.html#cb104-2" aria-hidden="true" tabindex="-1"></a><span class="co"># same above</span></span>
<span id="cb104-3"><a href="dplyr-functions.html#cb104-3" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%&gt;%</span> <span class="fu">group_by</span>(a, b) <span class="sc">%&gt;%</span> <span class="fu">summarise</span>(<span class="at">n =</span> <span class="fu">n</span>())</span></code></pre></div>
<div class="sourceCode" id="cb105"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb105-1"><a href="dplyr-functions.html#cb105-1" aria-hidden="true" tabindex="-1"></a>starwars <span class="sc">%&gt;%</span> <span class="fu">count</span>(species)</span>
<span id="cb105-2"><a href="dplyr-functions.html#cb105-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 38 x 2</span></span>
<span id="cb105-3"><a href="dplyr-functions.html#cb105-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   species      n</span></span>
<span id="cb105-4"><a href="dplyr-functions.html#cb105-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;    &lt;int&gt;</span></span>
<span id="cb105-5"><a href="dplyr-functions.html#cb105-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 Aleena       1</span></span>
<span id="cb105-6"><a href="dplyr-functions.html#cb105-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 Besalisk     1</span></span>
<span id="cb105-7"><a href="dplyr-functions.html#cb105-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 Cerean       1</span></span>
<span id="cb105-8"><a href="dplyr-functions.html#cb105-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 Chagrian     1</span></span>
<span id="cb105-9"><a href="dplyr-functions.html#cb105-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 Clawdite     1</span></span>
<span id="cb105-10"><a href="dplyr-functions.html#cb105-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 Droid        6</span></span>
<span id="cb105-11"><a href="dplyr-functions.html#cb105-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 32 more rows</span></span>
<span id="cb105-12"><a href="dplyr-functions.html#cb105-12" aria-hidden="true" tabindex="-1"></a><span class="co"># same above 等价</span></span>
<span id="cb105-13"><a href="dplyr-functions.html#cb105-13" aria-hidden="true" tabindex="-1"></a>starwars <span class="sc">%&gt;%</span> <span class="fu">group_by</span>(species) <span class="sc">%&gt;%</span> <span class="fu">summarise</span>(<span class="at">n =</span> <span class="fu">n</span>())</span>
<span id="cb105-14"><a href="dplyr-functions.html#cb105-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 38 x 2</span></span>
<span id="cb105-15"><a href="dplyr-functions.html#cb105-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   species      n</span></span>
<span id="cb105-16"><a href="dplyr-functions.html#cb105-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;    &lt;int&gt;</span></span>
<span id="cb105-17"><a href="dplyr-functions.html#cb105-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 Aleena       1</span></span>
<span id="cb105-18"><a href="dplyr-functions.html#cb105-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 Besalisk     1</span></span>
<span id="cb105-19"><a href="dplyr-functions.html#cb105-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 Cerean       1</span></span>
<span id="cb105-20"><a href="dplyr-functions.html#cb105-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 Chagrian     1</span></span>
<span id="cb105-21"><a href="dplyr-functions.html#cb105-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 Clawdite     1</span></span>
<span id="cb105-22"><a href="dplyr-functions.html#cb105-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 Droid        6</span></span>
<span id="cb105-23"><a href="dplyr-functions.html#cb105-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 32 more rows</span></span></code></pre></div>
<ul>
<li>非重复计数</li>
</ul>
<p><code>n_distinct()</code>与<code>length(unique(x))</code>等价，但是更快更简洁。当我们需要给门店或订单之类数据去重计算时采用该函数。</p>
<div class="sourceCode" id="cb106"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb106-1"><a href="dplyr-functions.html#cb106-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>, <span class="fl">1e5</span>, <span class="at">rep =</span> <span class="cn">TRUE</span>)</span>
<span id="cb106-2"><a href="dplyr-functions.html#cb106-2" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span>(<span class="fu">unique</span>(x))</span>
<span id="cb106-3"><a href="dplyr-functions.html#cb106-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 10</span></span>
<span id="cb106-4"><a href="dplyr-functions.html#cb106-4" aria-hidden="true" tabindex="-1"></a><span class="fu">n_distinct</span>(x)</span>
<span id="cb106-5"><a href="dplyr-functions.html#cb106-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 10</span></span></code></pre></div>
</div>
<div id="排序函数" class="section level3" number="2.3.4">
<h3><span class="header-section-number">2.3.4</span> 排序函数</h3>
<p><code>dplyr</code>共六种排序函数，模仿SQL2003中的排名函数。</p>
<ul>
<li>row_number():等于 rank(ties.method = “first”)</li>
<li>min_rank(): 等于 rank(ties.method = “min”)</li>
<li>dense_rank(): 与min_rank()相似,但是没有间隔</li>
<li>percent_rank():返回0，1之间，通过min_rank()返回值缩放至[0,1]</li>
</ul>
<div class="sourceCode" id="cb107"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb107-1"><a href="dplyr-functions.html#cb107-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">5</span>, <span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">2</span>, <span class="cn">NA</span>)</span>
<span id="cb107-2"><a href="dplyr-functions.html#cb107-2" aria-hidden="true" tabindex="-1"></a><span class="fu">row_number</span>(x)</span>
<span id="cb107-3"><a href="dplyr-functions.html#cb107-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1]  5  1  4  2  3 NA</span></span>
<span id="cb107-4"><a href="dplyr-functions.html#cb107-4" aria-hidden="true" tabindex="-1"></a><span class="fu">min_rank</span>(x)</span>
<span id="cb107-5"><a href="dplyr-functions.html#cb107-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1]  5  1  4  2  2 NA</span></span>
<span id="cb107-6"><a href="dplyr-functions.html#cb107-6" aria-hidden="true" tabindex="-1"></a><span class="fu">dense_rank</span>(x)</span>
<span id="cb107-7"><a href="dplyr-functions.html#cb107-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1]  4  1  3  2  2 NA</span></span>
<span id="cb107-8"><a href="dplyr-functions.html#cb107-8" aria-hidden="true" tabindex="-1"></a><span class="fu">percent_rank</span>(x)</span>
<span id="cb107-9"><a href="dplyr-functions.html#cb107-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 1.00 0.00 0.75 0.25 0.25   NA</span></span>
<span id="cb107-10"><a href="dplyr-functions.html#cb107-10" aria-hidden="true" tabindex="-1"></a><span class="fu">cume_dist</span>(x)</span>
<span id="cb107-11"><a href="dplyr-functions.html#cb107-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 1.0 0.2 0.8 0.6 0.6  NA</span></span></code></pre></div>
</div>
<div id="提取向量" class="section level3" number="2.3.5">
<h3><span class="header-section-number">2.3.5</span> 提取向量</h3>
<p>该系列函数是对<code>[[</code>的包装，方便提取向量。</p>
<div class="sourceCode" id="cb108"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb108-1"><a href="dplyr-functions.html#cb108-1" aria-hidden="true" tabindex="-1"></a><span class="fu">nth</span>(x, n, <span class="at">order_by =</span> <span class="cn">NULL</span>, <span class="at">default =</span> <span class="fu">default_missing</span>(x))</span>
<span id="cb108-2"><a href="dplyr-functions.html#cb108-2" aria-hidden="true" tabindex="-1"></a><span class="fu">first</span>(x, <span class="at">order_by =</span> <span class="cn">NULL</span>, <span class="at">default =</span> <span class="fu">default_missing</span>(x))</span>
<span id="cb108-3"><a href="dplyr-functions.html#cb108-3" aria-hidden="true" tabindex="-1"></a><span class="fu">last</span>(x, <span class="at">order_by =</span> <span class="cn">NULL</span>, <span class="at">default =</span> <span class="fu">default_missing</span>(x))</span></code></pre></div>
<div class="sourceCode" id="cb109"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb109-1"><a href="dplyr-functions.html#cb109-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span></span>
<span id="cb109-2"><a href="dplyr-functions.html#cb109-2" aria-hidden="true" tabindex="-1"></a>y <span class="ot">&lt;-</span> <span class="dv">10</span><span class="sc">:</span><span class="dv">1</span></span>
<span id="cb109-3"><a href="dplyr-functions.html#cb109-3" aria-hidden="true" tabindex="-1"></a><span class="fu">first</span>(x)</span>
<span id="cb109-4"><a href="dplyr-functions.html#cb109-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 1</span></span>
<span id="cb109-5"><a href="dplyr-functions.html#cb109-5" aria-hidden="true" tabindex="-1"></a><span class="fu">last</span>(y)</span>
<span id="cb109-6"><a href="dplyr-functions.html#cb109-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 1</span></span>
<span id="cb109-7"><a href="dplyr-functions.html#cb109-7" aria-hidden="true" tabindex="-1"></a><span class="fu">nth</span>(x, <span class="dv">1</span>)</span>
<span id="cb109-8"><a href="dplyr-functions.html#cb109-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 1</span></span>
<span id="cb109-9"><a href="dplyr-functions.html#cb109-9" aria-hidden="true" tabindex="-1"></a><span class="fu">nth</span>(x, <span class="dv">5</span>)</span>
<span id="cb109-10"><a href="dplyr-functions.html#cb109-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 5</span></span></code></pre></div>
</div>
<div id="dplyr:slice" class="section level3" number="2.3.6">
<h3><span class="header-section-number">2.3.6</span> slice 系列</h3>
<p><code>slice()</code>函数让我们按照行数切片，让我们筛选，删除、重复某些行。和 python 中的 pandas 库的切片类似。 <code>slice()</code>共有如下几组函数：</p>
<ul>
<li><p>slice_head() 和 slice_tail()，选着头和尾行 。</p></li>
<li><p>slice_sample() 随机选择行。</p></li>
<li><p>slice_min() 和 slice_max() 按照变量的最低值和最高值选择行。</p></li>
</ul>
<p>如果用于 slice() 系列函数的数据集.data 是被 group_by() 过，那每个函数将作用在每一组上。例如，按照商品大类取每个商品大类前五的商品。</p>
<div class="sourceCode" id="cb110"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb110-1"><a href="dplyr-functions.html#cb110-1" aria-hidden="true" tabindex="-1"></a>dt <span class="sc">%&gt;%</span> </span>
<span id="cb110-2"><a href="dplyr-functions.html#cb110-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_by</span>(商品大类) <span class="sc">%&gt;%</span> </span>
<span id="cb110-3"><a href="dplyr-functions.html#cb110-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">slice_head</span>(<span class="at">n =</span> <span class="dv">5</span>)</span></code></pre></div>
<div id="函数用法" class="section level4" number="2.3.6.1">
<h4><span class="header-section-number">2.3.6.1</span> 函数用法</h4>
<div class="sourceCode" id="cb111"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb111-1"><a href="dplyr-functions.html#cb111-1" aria-hidden="true" tabindex="-1"></a><span class="fu">slice</span>(.data, ..., <span class="at">.preserve =</span> <span class="cn">FALSE</span>)</span>
<span id="cb111-2"><a href="dplyr-functions.html#cb111-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb111-3"><a href="dplyr-functions.html#cb111-3" aria-hidden="true" tabindex="-1"></a><span class="fu">slice_head</span>(.data, ..., n, prop)</span>
<span id="cb111-4"><a href="dplyr-functions.html#cb111-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb111-5"><a href="dplyr-functions.html#cb111-5" aria-hidden="true" tabindex="-1"></a><span class="fu">slice_tail</span>(.data, ..., n, prop)</span>
<span id="cb111-6"><a href="dplyr-functions.html#cb111-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb111-7"><a href="dplyr-functions.html#cb111-7" aria-hidden="true" tabindex="-1"></a><span class="fu">slice_min</span>(.data, order_by, ..., n, prop, <span class="at">with_ties =</span> <span class="cn">TRUE</span>)</span>
<span id="cb111-8"><a href="dplyr-functions.html#cb111-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb111-9"><a href="dplyr-functions.html#cb111-9" aria-hidden="true" tabindex="-1"></a><span class="fu">slice_max</span>(.data, order_by, ..., n, prop, <span class="at">with_ties =</span> <span class="cn">TRUE</span>)</span>
<span id="cb111-10"><a href="dplyr-functions.html#cb111-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb111-11"><a href="dplyr-functions.html#cb111-11" aria-hidden="true" tabindex="-1"></a><span class="fu">slice_sample</span>(.data, ..., n, prop, <span class="at">weight_by =</span> <span class="cn">NULL</span>, <span class="at">replace =</span> <span class="cn">FALSE</span>)</span></code></pre></div>
</div>
<div id="参数解释" class="section level4" number="2.3.6.2">
<h4><span class="header-section-number">2.3.6.2</span> 参数解释</h4>
<p>.data: 一个 data.frame , data frame extension (tibble)</p>
<p>…: 必须是整数，如果是正数将保留，负值将删除。提供的值超过行数的将会被忽略</p>
<p>.preserve :默认为FALSE,根据结果数据重新计算分组结构，否则保持原样分组</p>
<p>n,p: 提供要选择的行数 n,或行的比列比如 prop = 0.4，如果都未提供则默认n = 1</p>
<p>order_by: 要排序的变量或者是被函数作用的变量</p>
<p>with_ties: 针对 _min 或 _max 函数，相等时候是否强制输出指定行数。</p>
<p>weight_by: 抽样的权重</p>
<p>replace: 是否允许重复抽样，默认为FALSE</p>
</div>
<div id="slice-案例" class="section level4" number="2.3.6.3">
<h4><span class="header-section-number">2.3.6.3</span> slice 案例</h4>
<ul>
<li>slice()</li>
</ul>
<div class="sourceCode" id="cb112"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb112-1"><a href="dplyr-functions.html#cb112-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 筛选第一行</span></span>
<span id="cb112-2"><a href="dplyr-functions.html#cb112-2" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice</span>(1L)</span>
<span id="cb112-3"><a href="dplyr-functions.html#cb112-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 1 x 11</span></span>
<span id="cb112-4"><a href="dplyr-functions.html#cb112-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb112-5"><a href="dplyr-functions.html#cb112-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb112-6"><a href="dplyr-functions.html#cb112-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1    21     6   160   110   3.9  2.62  16.5     0     1     4     4</span></span>
<span id="cb112-7"><a href="dplyr-functions.html#cb112-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb112-8"><a href="dplyr-functions.html#cb112-8" aria-hidden="true" tabindex="-1"></a><span class="co"># 筛选最后一行</span></span>
<span id="cb112-9"><a href="dplyr-functions.html#cb112-9" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice</span>(<span class="fu">n</span>())</span>
<span id="cb112-10"><a href="dplyr-functions.html#cb112-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 1 x 11</span></span>
<span id="cb112-11"><a href="dplyr-functions.html#cb112-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb112-12"><a href="dplyr-functions.html#cb112-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb112-13"><a href="dplyr-functions.html#cb112-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  21.4     4   121   109  4.11  2.78  18.6     1     1     4     2</span></span>
<span id="cb112-14"><a href="dplyr-functions.html#cb112-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb112-15"><a href="dplyr-functions.html#cb112-15" aria-hidden="true" tabindex="-1"></a><span class="co"># 筛选5至最后一行</span></span>
<span id="cb112-16"><a href="dplyr-functions.html#cb112-16" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice</span>(<span class="dv">5</span><span class="sc">:</span><span class="fu">n</span>())</span>
<span id="cb112-17"><a href="dplyr-functions.html#cb112-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 28 x 11</span></span>
<span id="cb112-18"><a href="dplyr-functions.html#cb112-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb112-19"><a href="dplyr-functions.html#cb112-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb112-20"><a href="dplyr-functions.html#cb112-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2</span></span>
<span id="cb112-21"><a href="dplyr-functions.html#cb112-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1</span></span>
<span id="cb112-22"><a href="dplyr-functions.html#cb112-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4</span></span>
<span id="cb112-23"><a href="dplyr-functions.html#cb112-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  24.4     4  147.    62  3.69  3.19  20       1     0     4     2</span></span>
<span id="cb112-24"><a href="dplyr-functions.html#cb112-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2</span></span>
<span id="cb112-25"><a href="dplyr-functions.html#cb112-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4</span></span>
<span id="cb112-26"><a href="dplyr-functions.html#cb112-26" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 22 more rows</span></span>
<span id="cb112-27"><a href="dplyr-functions.html#cb112-27" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb112-28"><a href="dplyr-functions.html#cb112-28" aria-hidden="true" tabindex="-1"></a><span class="co"># 删除前面四行</span></span>
<span id="cb112-29"><a href="dplyr-functions.html#cb112-29" aria-hidden="true" tabindex="-1"></a><span class="fu">slice</span>(mtcars, <span class="sc">-</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>))</span>
<span id="cb112-30"><a href="dplyr-functions.html#cb112-30" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 28 x 11</span></span>
<span id="cb112-31"><a href="dplyr-functions.html#cb112-31" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb112-32"><a href="dplyr-functions.html#cb112-32" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb112-33"><a href="dplyr-functions.html#cb112-33" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2</span></span>
<span id="cb112-34"><a href="dplyr-functions.html#cb112-34" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1</span></span>
<span id="cb112-35"><a href="dplyr-functions.html#cb112-35" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4</span></span>
<span id="cb112-36"><a href="dplyr-functions.html#cb112-36" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  24.4     4  147.    62  3.69  3.19  20       1     0     4     2</span></span>
<span id="cb112-37"><a href="dplyr-functions.html#cb112-37" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2</span></span>
<span id="cb112-38"><a href="dplyr-functions.html#cb112-38" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4</span></span>
<span id="cb112-39"><a href="dplyr-functions.html#cb112-39" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 22 more rows</span></span></code></pre></div>
<ul>
<li>slice_head slice_tail</li>
</ul>
<div class="sourceCode" id="cb113"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb113-1"><a href="dplyr-functions.html#cb113-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 基于现有顺序筛选前面行或最后行</span></span>
<span id="cb113-2"><a href="dplyr-functions.html#cb113-2" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice_head</span>(<span class="at">n =</span> <span class="dv">5</span>)</span>
<span id="cb113-3"><a href="dplyr-functions.html#cb113-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5 x 11</span></span>
<span id="cb113-4"><a href="dplyr-functions.html#cb113-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb113-5"><a href="dplyr-functions.html#cb113-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb113-6"><a href="dplyr-functions.html#cb113-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  21       6   160   110  3.9   2.62  16.5     0     1     4     4</span></span>
<span id="cb113-7"><a href="dplyr-functions.html#cb113-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  21       6   160   110  3.9   2.88  17.0     0     1     4     4</span></span>
<span id="cb113-8"><a href="dplyr-functions.html#cb113-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  22.8     4   108    93  3.85  2.32  18.6     1     1     4     1</span></span>
<span id="cb113-9"><a href="dplyr-functions.html#cb113-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1</span></span>
<span id="cb113-10"><a href="dplyr-functions.html#cb113-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2</span></span>
<span id="cb113-11"><a href="dplyr-functions.html#cb113-11" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice_tail</span>(<span class="at">n =</span> <span class="dv">5</span>)</span>
<span id="cb113-12"><a href="dplyr-functions.html#cb113-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5 x 11</span></span>
<span id="cb113-13"><a href="dplyr-functions.html#cb113-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb113-14"><a href="dplyr-functions.html#cb113-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb113-15"><a href="dplyr-functions.html#cb113-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2</span></span>
<span id="cb113-16"><a href="dplyr-functions.html#cb113-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  15.8     8 351     264  4.22  3.17  14.5     0     1     5     4</span></span>
<span id="cb113-17"><a href="dplyr-functions.html#cb113-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  19.7     6 145     175  3.62  2.77  15.5     0     1     5     6</span></span>
<span id="cb113-18"><a href="dplyr-functions.html#cb113-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  15       8 301     335  3.54  3.57  14.6     0     1     5     8</span></span>
<span id="cb113-19"><a href="dplyr-functions.html#cb113-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  21.4     4 121     109  4.11  2.78  18.6     1     1     4     2</span></span></code></pre></div>
<ul>
<li>slice_min slice_max</li>
</ul>
<div class="sourceCode" id="cb114"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb114-1"><a href="dplyr-functions.html#cb114-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 基于变量筛选</span></span>
<span id="cb114-2"><a href="dplyr-functions.html#cb114-2" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice_min</span>(mpg, <span class="at">n =</span> <span class="dv">5</span>) <span class="co">#最小的五行</span></span>
<span id="cb114-3"><a href="dplyr-functions.html#cb114-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5 x 11</span></span>
<span id="cb114-4"><a href="dplyr-functions.html#cb114-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb114-5"><a href="dplyr-functions.html#cb114-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb114-6"><a href="dplyr-functions.html#cb114-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  10.4     8   472   205  2.93  5.25  18.0     0     0     3     4</span></span>
<span id="cb114-7"><a href="dplyr-functions.html#cb114-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  10.4     8   460   215  3     5.42  17.8     0     0     3     4</span></span>
<span id="cb114-8"><a href="dplyr-functions.html#cb114-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  13.3     8   350   245  3.73  3.84  15.4     0     0     3     4</span></span>
<span id="cb114-9"><a href="dplyr-functions.html#cb114-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  14.3     8   360   245  3.21  3.57  15.8     0     0     3     4</span></span>
<span id="cb114-10"><a href="dplyr-functions.html#cb114-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  14.7     8   440   230  3.23  5.34  17.4     0     0     3     4</span></span>
<span id="cb114-11"><a href="dplyr-functions.html#cb114-11" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice_max</span>(mpg, <span class="at">n =</span> <span class="dv">5</span>) <span class="co">#最大的五行</span></span>
<span id="cb114-12"><a href="dplyr-functions.html#cb114-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5 x 11</span></span>
<span id="cb114-13"><a href="dplyr-functions.html#cb114-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb114-14"><a href="dplyr-functions.html#cb114-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb114-15"><a href="dplyr-functions.html#cb114-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1</span></span>
<span id="cb114-16"><a href="dplyr-functions.html#cb114-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1</span></span>
<span id="cb114-17"><a href="dplyr-functions.html#cb114-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2</span></span>
<span id="cb114-18"><a href="dplyr-functions.html#cb114-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2</span></span>
<span id="cb114-19"><a href="dplyr-functions.html#cb114-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  27.3     4  79      66  4.08  1.94  18.9     1     1     4     1</span></span>
<span id="cb114-20"><a href="dplyr-functions.html#cb114-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb114-21"><a href="dplyr-functions.html#cb114-21" aria-hidden="true" tabindex="-1"></a><span class="co"># slice_min()可能返回更多行，通过with_ties参数控制</span></span>
<span id="cb114-22"><a href="dplyr-functions.html#cb114-22" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice_min</span>(cyl, <span class="at">n =</span> <span class="dv">1</span>)</span>
<span id="cb114-23"><a href="dplyr-functions.html#cb114-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 11 x 11</span></span>
<span id="cb114-24"><a href="dplyr-functions.html#cb114-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb114-25"><a href="dplyr-functions.html#cb114-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb114-26"><a href="dplyr-functions.html#cb114-26" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  22.8     4 108      93  3.85  2.32  18.6     1     1     4     1</span></span>
<span id="cb114-27"><a href="dplyr-functions.html#cb114-27" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  24.4     4 147.     62  3.69  3.19  20       1     0     4     2</span></span>
<span id="cb114-28"><a href="dplyr-functions.html#cb114-28" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  22.8     4 141.     95  3.92  3.15  22.9     1     0     4     2</span></span>
<span id="cb114-29"><a href="dplyr-functions.html#cb114-29" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1</span></span>
<span id="cb114-30"><a href="dplyr-functions.html#cb114-30" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2</span></span>
<span id="cb114-31"><a href="dplyr-functions.html#cb114-31" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1</span></span>
<span id="cb114-32"><a href="dplyr-functions.html#cb114-32" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 5 more rows</span></span>
<span id="cb114-33"><a href="dplyr-functions.html#cb114-33" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice_min</span>(cyl, <span class="at">n =</span> <span class="dv">1</span>, <span class="at">with_ties =</span> <span class="cn">FALSE</span>)</span>
<span id="cb114-34"><a href="dplyr-functions.html#cb114-34" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 1 x 11</span></span>
<span id="cb114-35"><a href="dplyr-functions.html#cb114-35" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb114-36"><a href="dplyr-functions.html#cb114-36" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb114-37"><a href="dplyr-functions.html#cb114-37" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  22.8     4   108    93  3.85  2.32  18.6     1     1     4     1</span></span></code></pre></div>
<ul>
<li>slice_sample</li>
</ul>
<p>在数据集中抽样，通过 replace 参数控制是否可以重复。</p>
<div class="sourceCode" id="cb115"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb115-1"><a href="dplyr-functions.html#cb115-1" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice_sample</span>(<span class="at">n =</span> <span class="dv">5</span>)</span>
<span id="cb115-2"><a href="dplyr-functions.html#cb115-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5 x 11</span></span>
<span id="cb115-3"><a href="dplyr-functions.html#cb115-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb115-4"><a href="dplyr-functions.html#cb115-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb115-5"><a href="dplyr-functions.html#cb115-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  19.2     6 168.    123  3.92  3.44  18.3     1     0     4     4</span></span>
<span id="cb115-6"><a href="dplyr-functions.html#cb115-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  10.4     8 472     205  2.93  5.25  18.0     0     0     3     4</span></span>
<span id="cb115-7"><a href="dplyr-functions.html#cb115-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2</span></span>
<span id="cb115-8"><a href="dplyr-functions.html#cb115-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  27.3     4  79      66  4.08  1.94  18.9     1     1     4     1</span></span>
<span id="cb115-9"><a href="dplyr-functions.html#cb115-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  13.3     8 350     245  3.73  3.84  15.4     0     0     3     4</span></span>
<span id="cb115-10"><a href="dplyr-functions.html#cb115-10" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice_sample</span>(<span class="at">n =</span> <span class="dv">5</span>, <span class="at">replace =</span> <span class="cn">TRUE</span>)</span>
<span id="cb115-11"><a href="dplyr-functions.html#cb115-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5 x 11</span></span>
<span id="cb115-12"><a href="dplyr-functions.html#cb115-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb115-13"><a href="dplyr-functions.html#cb115-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb115-14"><a href="dplyr-functions.html#cb115-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  21.5     4  120.    97  3.7   2.46  20.0     1     0     3     1</span></span>
<span id="cb115-15"><a href="dplyr-functions.html#cb115-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  14.7     8  440    230  3.23  5.34  17.4     0     0     3     4</span></span>
<span id="cb115-16"><a href="dplyr-functions.html#cb115-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  14.7     8  440    230  3.23  5.34  17.4     0     0     3     4</span></span>
<span id="cb115-17"><a href="dplyr-functions.html#cb115-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  17.3     8  276.   180  3.07  3.73  17.6     0     0     3     3</span></span>
<span id="cb115-18"><a href="dplyr-functions.html#cb115-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1</span></span></code></pre></div>
<p>weight_by 参数调整抽样权重</p>
<div class="sourceCode" id="cb116"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb116-1"><a href="dplyr-functions.html#cb116-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 重量大的会更容易抽到</span></span>
<span id="cb116-2"><a href="dplyr-functions.html#cb116-2" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">slice_sample</span>(<span class="at">weight_by =</span> wt, <span class="at">n =</span> <span class="dv">5</span>)</span>
<span id="cb116-3"><a href="dplyr-functions.html#cb116-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5 x 11</span></span>
<span id="cb116-4"><a href="dplyr-functions.html#cb116-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb116-5"><a href="dplyr-functions.html#cb116-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb116-6"><a href="dplyr-functions.html#cb116-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  21.4     4   121   109  4.11  2.78  18.6     1     1     4     2</span></span>
<span id="cb116-7"><a href="dplyr-functions.html#cb116-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  21       6   160   110  3.9   2.88  17.0     0     1     4     4</span></span>
<span id="cb116-8"><a href="dplyr-functions.html#cb116-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  15.5     8   318   150  2.76  3.52  16.9     0     0     3     2</span></span>
<span id="cb116-9"><a href="dplyr-functions.html#cb116-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  21       6   160   110  3.9   2.62  16.5     0     1     4     4</span></span>
<span id="cb116-10"><a href="dplyr-functions.html#cb116-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  10.4     8   472   205  2.93  5.25  18.0     0     0     3     4</span></span></code></pre></div>
<ul>
<li>分组操作</li>
</ul>
<div class="sourceCode" id="cb117"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb117-1"><a href="dplyr-functions.html#cb117-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb117-2"><a href="dplyr-functions.html#cb117-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">group =</span> <span class="fu">rep</span>(<span class="fu">c</span>(<span class="st">&quot;a&quot;</span>, <span class="st">&quot;b&quot;</span>, <span class="st">&quot;c&quot;</span>), <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>)),</span>
<span id="cb117-3"><a href="dplyr-functions.html#cb117-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">x =</span> <span class="fu">runif</span>(<span class="dv">7</span>)</span>
<span id="cb117-4"><a href="dplyr-functions.html#cb117-4" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb117-5"><a href="dplyr-functions.html#cb117-5" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%&gt;%</span> <span class="fu">group_by</span>(group) <span class="sc">%&gt;%</span> <span class="fu">slice_head</span>(<span class="at">n =</span> <span class="dv">2</span>)</span>
<span id="cb117-6"><a href="dplyr-functions.html#cb117-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5 x 2</span></span>
<span id="cb117-7"><a href="dplyr-functions.html#cb117-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # Groups:   group [3]</span></span>
<span id="cb117-8"><a href="dplyr-functions.html#cb117-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   group     x</span></span>
<span id="cb117-9"><a href="dplyr-functions.html#cb117-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt; &lt;dbl&gt;</span></span>
<span id="cb117-10"><a href="dplyr-functions.html#cb117-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 a     0.810</span></span>
<span id="cb117-11"><a href="dplyr-functions.html#cb117-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 b     0.802</span></span>
<span id="cb117-12"><a href="dplyr-functions.html#cb117-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 b     0.860</span></span>
<span id="cb117-13"><a href="dplyr-functions.html#cb117-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 c     0.344</span></span>
<span id="cb117-14"><a href="dplyr-functions.html#cb117-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 c     0.683</span></span>
<span id="cb117-15"><a href="dplyr-functions.html#cb117-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb117-16"><a href="dplyr-functions.html#cb117-16" aria-hidden="true" tabindex="-1"></a><span class="co"># 注意体会使用prop参数时的差异</span></span>
<span id="cb117-17"><a href="dplyr-functions.html#cb117-17" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%&gt;%</span> <span class="fu">group_by</span>(group) <span class="sc">%&gt;%</span> <span class="fu">slice_head</span>(<span class="at">prop =</span> <span class="fl">0.4</span>)  <span class="co">#仅c组返回一个，因为c组3个数字的 40%的前面还有数</span></span>
<span id="cb117-18"><a href="dplyr-functions.html#cb117-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 1 x 2</span></span>
<span id="cb117-19"><a href="dplyr-functions.html#cb117-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # Groups:   group [1]</span></span>
<span id="cb117-20"><a href="dplyr-functions.html#cb117-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   group     x</span></span>
<span id="cb117-21"><a href="dplyr-functions.html#cb117-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt; &lt;dbl&gt;</span></span>
<span id="cb117-22"><a href="dplyr-functions.html#cb117-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 c     0.344</span></span>
<span id="cb117-23"><a href="dplyr-functions.html#cb117-23" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%&gt;%</span> <span class="fu">group_by</span>(group) <span class="sc">%&gt;%</span> <span class="fu">slice_head</span>(<span class="at">prop =</span> <span class="fl">0.5</span>) <span class="co"># 因为a组只有一个数字，前50%位没有数字</span></span>
<span id="cb117-24"><a href="dplyr-functions.html#cb117-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 x 2</span></span>
<span id="cb117-25"><a href="dplyr-functions.html#cb117-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # Groups:   group [2]</span></span>
<span id="cb117-26"><a href="dplyr-functions.html#cb117-26" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   group     x</span></span>
<span id="cb117-27"><a href="dplyr-functions.html#cb117-27" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt; &lt;dbl&gt;</span></span>
<span id="cb117-28"><a href="dplyr-functions.html#cb117-28" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 b     0.802</span></span>
<span id="cb117-29"><a href="dplyr-functions.html#cb117-29" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 c     0.344</span></span>
<span id="cb117-30"><a href="dplyr-functions.html#cb117-30" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 c     0.683</span></span></code></pre></div>
</div>
</div>
<div id="group-系列" class="section level3" number="2.3.7">
<h3><span class="header-section-number">2.3.7</span> group 系列</h3>
<p>group 系列函数包含　group_by(),group_map(), group_nest(), group_split(), group_trim()等。
其中我常用group_by(),group_split()两个函数。group_by()是我们熟悉的动词，大部分数据操作中的分组操作由它完成。</p>
<ul>
<li>group_by()</li>
</ul>
<div class="sourceCode" id="cb118"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb118-1"><a href="dplyr-functions.html#cb118-1" aria-hidden="true" tabindex="-1"></a><span class="co">#group_by()不会改变数据框</span></span>
<span id="cb118-2"><a href="dplyr-functions.html#cb118-2" aria-hidden="true" tabindex="-1"></a>by_cyl <span class="ot">&lt;-</span> mtcars <span class="sc">%&gt;%</span> <span class="fu">group_by</span>(cyl)</span>
<span id="cb118-3"><a href="dplyr-functions.html#cb118-3" aria-hidden="true" tabindex="-1"></a>by_cyl</span>
<span id="cb118-4"><a href="dplyr-functions.html#cb118-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 32 x 11</span></span>
<span id="cb118-5"><a href="dplyr-functions.html#cb118-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # Groups:   cyl [3]</span></span>
<span id="cb118-6"><a href="dplyr-functions.html#cb118-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb118-7"><a href="dplyr-functions.html#cb118-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb118-8"><a href="dplyr-functions.html#cb118-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  21       6   160   110  3.9   2.62  16.5     0     1     4     4</span></span>
<span id="cb118-9"><a href="dplyr-functions.html#cb118-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  21       6   160   110  3.9   2.88  17.0     0     1     4     4</span></span>
<span id="cb118-10"><a href="dplyr-functions.html#cb118-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  22.8     4   108    93  3.85  2.32  18.6     1     1     4     1</span></span>
<span id="cb118-11"><a href="dplyr-functions.html#cb118-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1</span></span>
<span id="cb118-12"><a href="dplyr-functions.html#cb118-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2</span></span>
<span id="cb118-13"><a href="dplyr-functions.html#cb118-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6  18.1     6   225   105  2.76  3.46  20.2     1     0     3     1</span></span>
<span id="cb118-14"><a href="dplyr-functions.html#cb118-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 26 more rows</span></span>
<span id="cb118-15"><a href="dplyr-functions.html#cb118-15" aria-hidden="true" tabindex="-1"></a><span class="co"># It changes how it acts with the other dplyr verbs:</span></span>
<span id="cb118-16"><a href="dplyr-functions.html#cb118-16" aria-hidden="true" tabindex="-1"></a>by_cyl <span class="sc">%&gt;%</span> <span class="fu">summarise</span>(</span>
<span id="cb118-17"><a href="dplyr-functions.html#cb118-17" aria-hidden="true" tabindex="-1"></a>  <span class="at">disp =</span> <span class="fu">mean</span>(disp),</span>
<span id="cb118-18"><a href="dplyr-functions.html#cb118-18" aria-hidden="true" tabindex="-1"></a>  <span class="at">hp =</span> <span class="fu">mean</span>(hp)</span>
<span id="cb118-19"><a href="dplyr-functions.html#cb118-19" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb118-20"><a href="dplyr-functions.html#cb118-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 x 3</span></span>
<span id="cb118-21"><a href="dplyr-functions.html#cb118-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     cyl  disp    hp</span></span>
<span id="cb118-22"><a href="dplyr-functions.html#cb118-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb118-23"><a href="dplyr-functions.html#cb118-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1     4  105.  82.6</span></span>
<span id="cb118-24"><a href="dplyr-functions.html#cb118-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2     6  183. 122. </span></span>
<span id="cb118-25"><a href="dplyr-functions.html#cb118-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3     8  353. 209.</span></span>
<span id="cb118-26"><a href="dplyr-functions.html#cb118-26" aria-hidden="true" tabindex="-1"></a><span class="co"># group_by中可以添加计算字段 即mutate操作</span></span>
<span id="cb118-27"><a href="dplyr-functions.html#cb118-27" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">group_by</span>(<span class="at">vsam =</span> vs <span class="sc">+</span> am) <span class="sc">%&gt;%</span></span>
<span id="cb118-28"><a href="dplyr-functions.html#cb118-28" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_vars</span>()</span>
<span id="cb118-29"><a href="dplyr-functions.html#cb118-29" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] &quot;vsam&quot;</span></span></code></pre></div>
<p>－ group_split()</p>
<p>目前该函数是实验性的，group_split() 功能与 base::split()相似，按照数据集的分组变量切割数据集。如下，将 iris 数据集按照 Species 分组并切割成长度为3的列表，每个子元素是 Species 变量的子数据集。Species 变量包含<code>setosa</code>、<code>versicolor</code>、<code>virginica</code>三种情况。</p>
<div class="sourceCode" id="cb119"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb119-1"><a href="dplyr-functions.html#cb119-1" aria-hidden="true" tabindex="-1"></a>ir <span class="ot">&lt;-</span> iris <span class="sc">%&gt;%</span></span>
<span id="cb119-2"><a href="dplyr-functions.html#cb119-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_by</span>(Species)</span>
<span id="cb119-3"><a href="dplyr-functions.html#cb119-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb119-4"><a href="dplyr-functions.html#cb119-4" aria-hidden="true" tabindex="-1"></a><span class="fu">group_split</span>(ir)</span>
<span id="cb119-5"><a href="dplyr-functions.html#cb119-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; &lt;list_of&lt;</span></span>
<span id="cb119-6"><a href="dplyr-functions.html#cb119-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   tbl_df&lt;</span></span>
<span id="cb119-7"><a href="dplyr-functions.html#cb119-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     Sepal.Length: double</span></span>
<span id="cb119-8"><a href="dplyr-functions.html#cb119-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     Sepal.Width : double</span></span>
<span id="cb119-9"><a href="dplyr-functions.html#cb119-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     Petal.Length: double</span></span>
<span id="cb119-10"><a href="dplyr-functions.html#cb119-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     Petal.Width : double</span></span>
<span id="cb119-11"><a href="dplyr-functions.html#cb119-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     Species     : factor&lt;fb977&gt;</span></span>
<span id="cb119-12"><a href="dplyr-functions.html#cb119-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &gt;</span></span>
<span id="cb119-13"><a href="dplyr-functions.html#cb119-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; &gt;[3]&gt;</span></span>
<span id="cb119-14"><a href="dplyr-functions.html#cb119-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [[1]]</span></span>
<span id="cb119-15"><a href="dplyr-functions.html#cb119-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 50 x 5</span></span>
<span id="cb119-16"><a href="dplyr-functions.html#cb119-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   Sepal.Length Sepal.Width Petal.Length Petal.Width Species</span></span>
<span id="cb119-17"><a href="dplyr-functions.html#cb119-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;          &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;fct&gt;  </span></span>
<span id="cb119-18"><a href="dplyr-functions.html#cb119-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1          5.1         3.5          1.4         0.2 setosa </span></span>
<span id="cb119-19"><a href="dplyr-functions.html#cb119-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2          4.9         3            1.4         0.2 setosa </span></span>
<span id="cb119-20"><a href="dplyr-functions.html#cb119-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3          4.7         3.2          1.3         0.2 setosa </span></span>
<span id="cb119-21"><a href="dplyr-functions.html#cb119-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4          4.6         3.1          1.5         0.2 setosa </span></span>
<span id="cb119-22"><a href="dplyr-functions.html#cb119-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5          5           3.6          1.4         0.2 setosa </span></span>
<span id="cb119-23"><a href="dplyr-functions.html#cb119-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6          5.4         3.9          1.7         0.4 setosa </span></span>
<span id="cb119-24"><a href="dplyr-functions.html#cb119-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 44 more rows</span></span>
<span id="cb119-25"><a href="dplyr-functions.html#cb119-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; </span></span>
<span id="cb119-26"><a href="dplyr-functions.html#cb119-26" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [[2]]</span></span>
<span id="cb119-27"><a href="dplyr-functions.html#cb119-27" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 50 x 5</span></span>
<span id="cb119-28"><a href="dplyr-functions.html#cb119-28" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   </span></span>
<span id="cb119-29"><a href="dplyr-functions.html#cb119-29" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;          &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;fct&gt;     </span></span>
<span id="cb119-30"><a href="dplyr-functions.html#cb119-30" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1          7           3.2          4.7         1.4 versicolor</span></span>
<span id="cb119-31"><a href="dplyr-functions.html#cb119-31" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2          6.4         3.2          4.5         1.5 versicolor</span></span>
<span id="cb119-32"><a href="dplyr-functions.html#cb119-32" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3          6.9         3.1          4.9         1.5 versicolor</span></span>
<span id="cb119-33"><a href="dplyr-functions.html#cb119-33" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4          5.5         2.3          4           1.3 versicolor</span></span>
<span id="cb119-34"><a href="dplyr-functions.html#cb119-34" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5          6.5         2.8          4.6         1.5 versicolor</span></span>
<span id="cb119-35"><a href="dplyr-functions.html#cb119-35" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6          5.7         2.8          4.5         1.3 versicolor</span></span>
<span id="cb119-36"><a href="dplyr-functions.html#cb119-36" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 44 more rows</span></span>
<span id="cb119-37"><a href="dplyr-functions.html#cb119-37" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; </span></span>
<span id="cb119-38"><a href="dplyr-functions.html#cb119-38" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [[3]]</span></span>
<span id="cb119-39"><a href="dplyr-functions.html#cb119-39" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 50 x 5</span></span>
<span id="cb119-40"><a href="dplyr-functions.html#cb119-40" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   Sepal.Length Sepal.Width Petal.Length Petal.Width Species  </span></span>
<span id="cb119-41"><a href="dplyr-functions.html#cb119-41" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;          &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;fct&gt;    </span></span>
<span id="cb119-42"><a href="dplyr-functions.html#cb119-42" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1          6.3         3.3          6           2.5 virginica</span></span>
<span id="cb119-43"><a href="dplyr-functions.html#cb119-43" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2          5.8         2.7          5.1         1.9 virginica</span></span>
<span id="cb119-44"><a href="dplyr-functions.html#cb119-44" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3          7.1         3            5.9         2.1 virginica</span></span>
<span id="cb119-45"><a href="dplyr-functions.html#cb119-45" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4          6.3         2.9          5.6         1.8 virginica</span></span>
<span id="cb119-46"><a href="dplyr-functions.html#cb119-46" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5          6.5         3            5.8         2.2 virginica</span></span>
<span id="cb119-47"><a href="dplyr-functions.html#cb119-47" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6          7.6         3            6.6         2.1 virginica</span></span>
<span id="cb119-48"><a href="dplyr-functions.html#cb119-48" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 44 more rows</span></span>
<span id="cb119-49"><a href="dplyr-functions.html#cb119-49" aria-hidden="true" tabindex="-1"></a><span class="fu">group_keys</span>(ir)</span>
<span id="cb119-50"><a href="dplyr-functions.html#cb119-50" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 x 1</span></span>
<span id="cb119-51"><a href="dplyr-functions.html#cb119-51" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   Species   </span></span>
<span id="cb119-52"><a href="dplyr-functions.html#cb119-52" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;     </span></span>
<span id="cb119-53"><a href="dplyr-functions.html#cb119-53" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 setosa    </span></span>
<span id="cb119-54"><a href="dplyr-functions.html#cb119-54" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 versicolor</span></span>
<span id="cb119-55"><a href="dplyr-functions.html#cb119-55" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 virginica</span></span></code></pre></div>
<ul>
<li>group_map()</li>
</ul>
<p>group_map，group_modify,group_walk等三个函数是purrr类具有迭代风格的函数。简单关系数据的数据清洗一般不涉及，常用在建模等方面。</p>
<p>但是目前函数是实验性的，未来可能会发生变化。</p>
<div class="sourceCode" id="cb120"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb120-1"><a href="dplyr-functions.html#cb120-1" aria-hidden="true" tabindex="-1"></a><span class="co"># return a list</span></span>
<span id="cb120-2"><a href="dplyr-functions.html#cb120-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 返回列表</span></span>
<span id="cb120-3"><a href="dplyr-functions.html#cb120-3" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span></span>
<span id="cb120-4"><a href="dplyr-functions.html#cb120-4" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_by</span>(cyl) <span class="sc">%&gt;%</span></span>
<span id="cb120-5"><a href="dplyr-functions.html#cb120-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_map</span>(<span class="sc">~</span> <span class="fu">head</span>(.x, 2L))</span>
<span id="cb120-6"><a href="dplyr-functions.html#cb120-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [[1]]</span></span>
<span id="cb120-7"><a href="dplyr-functions.html#cb120-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 2 x 10</span></span>
<span id="cb120-8"><a href="dplyr-functions.html#cb120-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb120-9"><a href="dplyr-functions.html#cb120-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb120-10"><a href="dplyr-functions.html#cb120-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  22.8  108     93  3.85  2.32  18.6     1     1     4     1</span></span>
<span id="cb120-11"><a href="dplyr-functions.html#cb120-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  24.4  147.    62  3.69  3.19  20       1     0     4     2</span></span>
<span id="cb120-12"><a href="dplyr-functions.html#cb120-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; </span></span>
<span id="cb120-13"><a href="dplyr-functions.html#cb120-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [[2]]</span></span>
<span id="cb120-14"><a href="dplyr-functions.html#cb120-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 2 x 10</span></span>
<span id="cb120-15"><a href="dplyr-functions.html#cb120-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb120-16"><a href="dplyr-functions.html#cb120-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb120-17"><a href="dplyr-functions.html#cb120-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1    21   160   110   3.9  2.62  16.5     0     1     4     4</span></span>
<span id="cb120-18"><a href="dplyr-functions.html#cb120-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2    21   160   110   3.9  2.88  17.0     0     1     4     4</span></span>
<span id="cb120-19"><a href="dplyr-functions.html#cb120-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; </span></span>
<span id="cb120-20"><a href="dplyr-functions.html#cb120-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [[3]]</span></span>
<span id="cb120-21"><a href="dplyr-functions.html#cb120-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 2 x 10</span></span>
<span id="cb120-22"><a href="dplyr-functions.html#cb120-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;     mpg  disp    hp  drat    wt  qsec    vs    am  gear  carb</span></span>
<span id="cb120-23"><a href="dplyr-functions.html#cb120-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb120-24"><a href="dplyr-functions.html#cb120-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  18.7   360   175  3.15  3.44  17.0     0     0     3     2</span></span>
<span id="cb120-25"><a href="dplyr-functions.html#cb120-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  14.3   360   245  3.21  3.57  15.8     0     0     3     4</span></span></code></pre></div>
<div class="sourceCode" id="cb121"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb121-1"><a href="dplyr-functions.html#cb121-1" aria-hidden="true" tabindex="-1"></a>iris <span class="sc">%&gt;%</span></span>
<span id="cb121-2"><a href="dplyr-functions.html#cb121-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_by</span>(Species) <span class="sc">%&gt;%</span></span>
<span id="cb121-3"><a href="dplyr-functions.html#cb121-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_modify</span>(<span class="sc">~</span> {</span>
<span id="cb121-4"><a href="dplyr-functions.html#cb121-4" aria-hidden="true" tabindex="-1"></a>    .x <span class="sc">%&gt;%</span></span>
<span id="cb121-5"><a href="dplyr-functions.html#cb121-5" aria-hidden="true" tabindex="-1"></a>      purrr<span class="sc">::</span><span class="fu">map_dfc</span>(fivenum) <span class="sc">%&gt;%</span></span>
<span id="cb121-6"><a href="dplyr-functions.html#cb121-6" aria-hidden="true" tabindex="-1"></a>      <span class="fu">mutate</span>(<span class="at">nms =</span> <span class="fu">c</span>(<span class="st">&quot;min&quot;</span>, <span class="st">&quot;Q1&quot;</span>, <span class="st">&quot;median&quot;</span>, <span class="st">&quot;Q3&quot;</span>, <span class="st">&quot;max&quot;</span>))</span>
<span id="cb121-7"><a href="dplyr-functions.html#cb121-7" aria-hidden="true" tabindex="-1"></a>  })</span>
<span id="cb121-8"><a href="dplyr-functions.html#cb121-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 15 x 6</span></span>
<span id="cb121-9"><a href="dplyr-functions.html#cb121-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # Groups:   Species [3]</span></span>
<span id="cb121-10"><a href="dplyr-functions.html#cb121-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   Species    Sepal.Length Sepal.Width Petal.Length Petal.Width nms   </span></span>
<span id="cb121-11"><a href="dplyr-functions.html#cb121-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;             &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;chr&gt; </span></span>
<span id="cb121-12"><a href="dplyr-functions.html#cb121-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 setosa              4.3         2.3          1           0.1 min   </span></span>
<span id="cb121-13"><a href="dplyr-functions.html#cb121-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 setosa              4.8         3.2          1.4         0.2 Q1    </span></span>
<span id="cb121-14"><a href="dplyr-functions.html#cb121-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 setosa              5           3.4          1.5         0.2 median</span></span>
<span id="cb121-15"><a href="dplyr-functions.html#cb121-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 setosa              5.2         3.7          1.6         0.3 Q3    </span></span>
<span id="cb121-16"><a href="dplyr-functions.html#cb121-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 setosa              5.8         4.4          1.9         0.6 max   </span></span>
<span id="cb121-17"><a href="dplyr-functions.html#cb121-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 versicolor          4.9         2            3           1   min   </span></span>
<span id="cb121-18"><a href="dplyr-functions.html#cb121-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 9 more rows</span></span></code></pre></div>
<p>分组后批量输出</p>
<div class="sourceCode" id="cb122"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb122-1"><a href="dplyr-functions.html#cb122-1" aria-hidden="true" tabindex="-1"></a><span class="co"># group_walk</span></span>
<span id="cb122-2"><a href="dplyr-functions.html#cb122-2" aria-hidden="true" tabindex="-1"></a><span class="fu">dir.create</span>(temp <span class="ot">&lt;-</span> <span class="fu">tempfile</span>())</span>
<span id="cb122-3"><a href="dplyr-functions.html#cb122-3" aria-hidden="true" tabindex="-1"></a>iris <span class="sc">%&gt;%</span></span>
<span id="cb122-4"><a href="dplyr-functions.html#cb122-4" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_by</span>(Species) <span class="sc">%&gt;%</span></span>
<span id="cb122-5"><a href="dplyr-functions.html#cb122-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_walk</span>(<span class="sc">~</span> <span class="fu">write.csv</span>(.x, <span class="at">file =</span> <span class="fu">file.path</span>(temp, <span class="fu">paste0</span>(.y<span class="sc">$</span>Species, <span class="st">&quot;.csv&quot;</span>))))</span>
<span id="cb122-6"><a href="dplyr-functions.html#cb122-6" aria-hidden="true" tabindex="-1"></a><span class="fu">list.files</span>(temp, <span class="at">pattern =</span> <span class="st">&quot;csv$&quot;</span>)</span>
<span id="cb122-7"><a href="dplyr-functions.html#cb122-7" aria-hidden="true" tabindex="-1"></a><span class="fu">unlink</span>(temp, <span class="at">recursive =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<ul>
<li>group_cols()</li>
</ul>
<p>选择分组变量</p>
<div class="sourceCode" id="cb123"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb123-1"><a href="dplyr-functions.html#cb123-1" aria-hidden="true" tabindex="-1"></a>gdf <span class="ot">&lt;-</span> iris <span class="sc">%&gt;%</span> <span class="fu">group_by</span>(Species)</span>
<span id="cb123-2"><a href="dplyr-functions.html#cb123-2" aria-hidden="true" tabindex="-1"></a>gdf <span class="sc">%&gt;%</span> <span class="fu">select</span>(<span class="fu">group_cols</span>())</span>
<span id="cb123-3"><a href="dplyr-functions.html#cb123-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 150 x 1</span></span>
<span id="cb123-4"><a href="dplyr-functions.html#cb123-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # Groups:   Species [3]</span></span>
<span id="cb123-5"><a href="dplyr-functions.html#cb123-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   Species</span></span>
<span id="cb123-6"><a href="dplyr-functions.html#cb123-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;  </span></span>
<span id="cb123-7"><a href="dplyr-functions.html#cb123-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 setosa </span></span>
<span id="cb123-8"><a href="dplyr-functions.html#cb123-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 setosa </span></span>
<span id="cb123-9"><a href="dplyr-functions.html#cb123-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 setosa </span></span>
<span id="cb123-10"><a href="dplyr-functions.html#cb123-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 setosa </span></span>
<span id="cb123-11"><a href="dplyr-functions.html#cb123-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 setosa </span></span>
<span id="cb123-12"><a href="dplyr-functions.html#cb123-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 setosa </span></span>
<span id="cb123-13"><a href="dplyr-functions.html#cb123-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 144 more rows</span></span></code></pre></div>
</div>
<div id="其它函数" class="section level3" number="2.3.8">
<h3><span class="header-section-number">2.3.8</span> 其它函数</h3>
<p>有针对性学习函数可以有效提高学习效率，先浏览一遍 dplyr 中函数，再挑选觉得对自己有用的函数学习即可。</p>
<ul>
<li>between</li>
</ul>
<div class="sourceCode" id="cb124"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb124-1"><a href="dplyr-functions.html#cb124-1" aria-hidden="true" tabindex="-1"></a><span class="fu">between</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">12</span>, <span class="dv">7</span>, <span class="dv">9</span>)</span>
<span id="cb124-2"><a href="dplyr-functions.html#cb124-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE</span></span></code></pre></div>
<ul>
<li>pull</li>
</ul>
<div class="sourceCode" id="cb125"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb125-1"><a href="dplyr-functions.html#cb125-1" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">pull</span>(<span class="sc">-</span><span class="dv">1</span>)</span>
<span id="cb125-2"><a href="dplyr-functions.html#cb125-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [1] 4 4 1 1 2 1 4 2 2 4 4 3 3 3 4 4 4 1 2 1 1 2 2 4 2 1 2 2 4 6 8 2</span></span>
<span id="cb125-3"><a href="dplyr-functions.html#cb125-3" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%&gt;%</span> <span class="fu">pull</span>(cyl)</span>
<span id="cb125-4"><a href="dplyr-functions.html#cb125-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4</span></span></code></pre></div>
<ul>
<li>distinct</li>
</ul>
<div class="sourceCode" id="cb126"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb126-1"><a href="dplyr-functions.html#cb126-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb126-2"><a href="dplyr-functions.html#cb126-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">x =</span> <span class="fu">sample</span>(<span class="dv">10</span>, <span class="dv">100</span>, <span class="at">rep =</span> <span class="cn">TRUE</span>),</span>
<span id="cb126-3"><a href="dplyr-functions.html#cb126-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">y =</span> <span class="fu">sample</span>(<span class="dv">10</span>, <span class="dv">100</span>, <span class="at">rep =</span> <span class="cn">TRUE</span>)</span>
<span id="cb126-4"><a href="dplyr-functions.html#cb126-4" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb126-5"><a href="dplyr-functions.html#cb126-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb126-6"><a href="dplyr-functions.html#cb126-6" aria-hidden="true" tabindex="-1"></a><span class="fu">distinct</span>(df, x)</span>
<span id="cb126-7"><a href="dplyr-functions.html#cb126-7" aria-hidden="true" tabindex="-1"></a><span class="fu">distinct</span>(df, x, <span class="at">.keep_all =</span> <span class="cn">TRUE</span>)</span>
<span id="cb126-8"><a href="dplyr-functions.html#cb126-8" aria-hidden="true" tabindex="-1"></a><span class="fu">distinct</span>(df, <span class="at">diff =</span> <span class="fu">abs</span>(x <span class="sc">-</span> y))</span></code></pre></div>
<ul>
<li>lead lag</li>
</ul>
<p>在向量中查找“向前”或滞后的值，在比较当前值以及前后值时比较有用。</p>
<div class="sourceCode" id="cb127"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb127-1"><a href="dplyr-functions.html#cb127-1" aria-hidden="true" tabindex="-1"></a><span class="fu">lag</span>(x, <span class="at">n =</span> 1L, <span class="at">default =</span> <span class="cn">NA</span>, <span class="at">order_by =</span> <span class="cn">NULL</span>, ...)</span>
<span id="cb127-2"><a href="dplyr-functions.html#cb127-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb127-3"><a href="dplyr-functions.html#cb127-3" aria-hidden="true" tabindex="-1"></a><span class="fu">lead</span>(x, <span class="at">n =</span> 1L, <span class="at">default =</span> <span class="cn">NA</span>, <span class="at">order_by =</span> <span class="cn">NULL</span>, ...)</span></code></pre></div>
<div class="sourceCode" id="cb128"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb128-1"><a href="dplyr-functions.html#cb128-1" aria-hidden="true" tabindex="-1"></a><span class="fu">lag</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>)</span>
<span id="cb128-2"><a href="dplyr-functions.html#cb128-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] NA  1  2  3  4</span></span>
<span id="cb128-3"><a href="dplyr-functions.html#cb128-3" aria-hidden="true" tabindex="-1"></a><span class="fu">lead</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>)</span>
<span id="cb128-4"><a href="dplyr-functions.html#cb128-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1]  2  3  4  5 NA</span></span>
<span id="cb128-5"><a href="dplyr-functions.html#cb128-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb128-6"><a href="dplyr-functions.html#cb128-6" aria-hidden="true" tabindex="-1"></a><span class="fu">lag</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="at">n =</span> <span class="dv">1</span>)</span>
<span id="cb128-7"><a href="dplyr-functions.html#cb128-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] NA  1  2  3  4</span></span>
<span id="cb128-8"><a href="dplyr-functions.html#cb128-8" aria-hidden="true" tabindex="-1"></a><span class="fu">lag</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="at">n =</span> <span class="dv">2</span>)</span>
<span id="cb128-9"><a href="dplyr-functions.html#cb128-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] NA NA  1  2  3</span></span></code></pre></div>
<p>order_by 参数控制顺序。</p>
<div class="sourceCode" id="cb129"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb129-1"><a href="dplyr-functions.html#cb129-1" aria-hidden="true" tabindex="-1"></a><span class="co"># If data are not already ordered, use `order_by`</span></span>
<span id="cb129-2"><a href="dplyr-functions.html#cb129-2" aria-hidden="true" tabindex="-1"></a>scrambled <span class="ot">&lt;-</span> <span class="fu">slice_sample</span>(<span class="fu">tibble</span>(<span class="at">year =</span> <span class="dv">2000</span><span class="sc">:</span><span class="dv">2005</span>, <span class="at">value =</span> (<span class="dv">0</span><span class="sc">:</span><span class="dv">5</span>) <span class="sc">^</span> <span class="dv">2</span>), <span class="at">prop =</span> <span class="dv">1</span>)</span>
<span id="cb129-3"><a href="dplyr-functions.html#cb129-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb129-4"><a href="dplyr-functions.html#cb129-4" aria-hidden="true" tabindex="-1"></a>wrong <span class="ot">&lt;-</span> <span class="fu">mutate</span>(scrambled, <span class="at">previous_year_value =</span> <span class="fu">lag</span>(value))</span>
<span id="cb129-5"><a href="dplyr-functions.html#cb129-5" aria-hidden="true" tabindex="-1"></a><span class="fu">arrange</span>(wrong, year)</span>
<span id="cb129-6"><a href="dplyr-functions.html#cb129-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 6 x 3</span></span>
<span id="cb129-7"><a href="dplyr-functions.html#cb129-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;    year value previous_year_value</span></span>
<span id="cb129-8"><a href="dplyr-functions.html#cb129-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;int&gt; &lt;dbl&gt;               &lt;dbl&gt;</span></span>
<span id="cb129-9"><a href="dplyr-functions.html#cb129-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  2000     0                   1</span></span>
<span id="cb129-10"><a href="dplyr-functions.html#cb129-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  2001     1                  25</span></span>
<span id="cb129-11"><a href="dplyr-functions.html#cb129-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  2002     4                  NA</span></span>
<span id="cb129-12"><a href="dplyr-functions.html#cb129-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  2003     9                   4</span></span>
<span id="cb129-13"><a href="dplyr-functions.html#cb129-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  2004    16                   0</span></span>
<span id="cb129-14"><a href="dplyr-functions.html#cb129-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6  2005    25                   9</span></span>
<span id="cb129-15"><a href="dplyr-functions.html#cb129-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb129-16"><a href="dplyr-functions.html#cb129-16" aria-hidden="true" tabindex="-1"></a>right <span class="ot">&lt;-</span> <span class="fu">mutate</span>(scrambled, <span class="at">previous_year_value =</span> <span class="fu">lag</span>(value, <span class="at">order_by =</span> year))</span>
<span id="cb129-17"><a href="dplyr-functions.html#cb129-17" aria-hidden="true" tabindex="-1"></a><span class="fu">arrange</span>(right, year)</span>
<span id="cb129-18"><a href="dplyr-functions.html#cb129-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 6 x 3</span></span>
<span id="cb129-19"><a href="dplyr-functions.html#cb129-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;    year value previous_year_value</span></span>
<span id="cb129-20"><a href="dplyr-functions.html#cb129-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;int&gt; &lt;dbl&gt;               &lt;dbl&gt;</span></span>
<span id="cb129-21"><a href="dplyr-functions.html#cb129-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  2000     0                  NA</span></span>
<span id="cb129-22"><a href="dplyr-functions.html#cb129-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  2001     1                   0</span></span>
<span id="cb129-23"><a href="dplyr-functions.html#cb129-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  2002     4                   1</span></span>
<span id="cb129-24"><a href="dplyr-functions.html#cb129-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  2003     9                   4</span></span>
<span id="cb129-25"><a href="dplyr-functions.html#cb129-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  2004    16                   9</span></span>
<span id="cb129-26"><a href="dplyr-functions.html#cb129-26" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6  2005    25                  16</span></span></code></pre></div>
<blockquote>
<p>该函数在我们计算下单间隔天数时比较简洁方便</p>
</blockquote>
<p>如需了解更多，可以通过学习<a href="https://github.com/DavisVaughan/slider"><code>slider</code></a>包知道更多“滑动窗口函数”。</p>
<ul>
<li>cummean cumsum cumall cumany</li>
</ul>
<p>累计系列函数</p>
<div class="sourceCode" id="cb130"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb130-1"><a href="dplyr-functions.html#cb130-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">5</span>, <span class="dv">2</span>, <span class="dv">2</span>)</span>
<span id="cb130-2"><a href="dplyr-functions.html#cb130-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cummean</span>(x)</span>
<span id="cb130-3"><a href="dplyr-functions.html#cb130-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 1.00 2.00 3.00 2.75 2.60</span></span>
<span id="cb130-4"><a href="dplyr-functions.html#cb130-4" aria-hidden="true" tabindex="-1"></a><span class="fu">cumsum</span>(x) <span class="sc">/</span> <span class="fu">seq_along</span>(x)</span>
<span id="cb130-5"><a href="dplyr-functions.html#cb130-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 1.00 2.00 3.00 2.75 2.60</span></span>
<span id="cb130-6"><a href="dplyr-functions.html#cb130-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb130-7"><a href="dplyr-functions.html#cb130-7" aria-hidden="true" tabindex="-1"></a><span class="fu">cumall</span>(x <span class="sc">&lt;</span> <span class="dv">5</span>)</span>
<span id="cb130-8"><a href="dplyr-functions.html#cb130-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1]  TRUE  TRUE FALSE FALSE FALSE</span></span>
<span id="cb130-9"><a href="dplyr-functions.html#cb130-9" aria-hidden="true" tabindex="-1"></a><span class="fu">cumany</span>(x <span class="sc">==</span> <span class="dv">3</span>)</span>
<span id="cb130-10"><a href="dplyr-functions.html#cb130-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] FALSE  TRUE  TRUE  TRUE  TRUE</span></span></code></pre></div>
<ul>
<li>coalesce</li>
</ul>
<p>用一组向量值替换NAS，该函数受 SQL 的 COALESCE 函数启发。</p>
<div class="sourceCode" id="cb131"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb131-1"><a href="dplyr-functions.html#cb131-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">sample</span>(<span class="fu">c</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="cn">NA</span>, <span class="cn">NA</span>, <span class="cn">NA</span>))</span>
<span id="cb131-2"><a href="dplyr-functions.html#cb131-2" aria-hidden="true" tabindex="-1"></a><span class="fu">coalesce</span>(x, 0L)</span>
<span id="cb131-3"><a href="dplyr-functions.html#cb131-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 3 2 0 5 4 0 1 0</span></span></code></pre></div>
<div class="sourceCode" id="cb132"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb132-1"><a href="dplyr-functions.html#cb132-1" aria-hidden="true" tabindex="-1"></a>y <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="cn">NA</span>, <span class="cn">NA</span>, <span class="dv">5</span>)</span>
<span id="cb132-2"><a href="dplyr-functions.html#cb132-2" aria-hidden="true" tabindex="-1"></a>z <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="cn">NA</span>, <span class="cn">NA</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>)</span>
<span id="cb132-3"><a href="dplyr-functions.html#cb132-3" aria-hidden="true" tabindex="-1"></a><span class="fu">coalesce</span>(y, z)</span>
<span id="cb132-4"><a href="dplyr-functions.html#cb132-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; [1] 1 2 3 4 5</span></span></code></pre></div>
<p>在数据框中替换NA</p>
<div class="sourceCode" id="cb133"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb133-1"><a href="dplyr-functions.html#cb133-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> <span class="fu">tibble</span>( <span class="at">a=</span> <span class="fu">sample</span>(<span class="fu">c</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>,<span class="cn">NA</span>,<span class="cn">NA</span>),<span class="dv">10</span>,<span class="at">replace =</span> <span class="cn">TRUE</span>))</span>
<span id="cb133-2"><a href="dplyr-functions.html#cb133-2" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%&gt;%</span> </span>
<span id="cb133-3"><a href="dplyr-functions.html#cb133-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">a =</span> <span class="fu">coalesce</span>(a,<span class="dv">0</span>))</span>
<span id="cb133-4"><a href="dplyr-functions.html#cb133-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 10 x 1</span></span>
<span id="cb133-5"><a href="dplyr-functions.html#cb133-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;       a</span></span>
<span id="cb133-6"><a href="dplyr-functions.html#cb133-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;dbl&gt;</span></span>
<span id="cb133-7"><a href="dplyr-functions.html#cb133-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1     1</span></span>
<span id="cb133-8"><a href="dplyr-functions.html#cb133-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2     0</span></span>
<span id="cb133-9"><a href="dplyr-functions.html#cb133-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3     3</span></span>
<span id="cb133-10"><a href="dplyr-functions.html#cb133-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4     2</span></span>
<span id="cb133-11"><a href="dplyr-functions.html#cb133-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5     2</span></span>
<span id="cb133-12"><a href="dplyr-functions.html#cb133-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6     0</span></span>
<span id="cb133-13"><a href="dplyr-functions.html#cb133-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 4 more rows</span></span></code></pre></div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="dplyr-usage.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="dplyr-merge-two-table.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["Data-Handling-in-R.pdf", "Data-Handling-in-R.epub"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
